Command bound in a datatemplate executing in all ViewModels instances
I have a problem with Commands and Datatemplates in WPF and I don't know if it's a bug or a normal behavior.
I have a CustomControl containing a listview. This control is used in several views (instances of UserControl). The datatemplate of the listview contained in the control is:
<DataTemplate x:Key="StandardContentDisplayDataTemplate">
<Grid d:DesignWidth="193.333" d:DesignHeight="128.036" Margin="25">
<Button Style="{Binding ItemButtonStyle, RelativeSource={RelativeSource AncestorType={x:Type Control:ContentDisplay}}}"
Margin="0"
VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Center"
Command="{Binding DataContext.ItemTouchCommand, RelativeSource={RelativeSource AncestorType={x:Type Control:ContentDisplay}}}"
CommandParameter="{Binding Id}"
Background="{Binding HexadecimalColor, FallbackValue=#FFAAAA}" BorderThickness="0" HorizontalAlignment="Stretch" Padding="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name, FallbackValue=Mon Item}" HorizontalAlignment="Center" VerticalAlignment="Center" ToolTipService.ToolTip="{Binding Name}" FontSize="14.667" Margin="0" Grid.Row="0"/>
<Image Margin="1,0,1,1" Grid.Row="1" VerticalAlignment="Stretch" Source="{Binding ImageUrl, FallbackValue=/logo.png, TargetNullValue=/logont.png}" Stretch="Uniform" />
</Grid>
</Button>
</Grid>
</DataTemplate>
The thing I do is binding the command to the datacontext of the ContentDisplay (which is my CustomControl) that is to say, my ViewModel.
The action linked to this command is a navigation to an开发者_开发百科other view containing the same control and also a command in its ViewModel, bound the same way (It has a total of 3 levels).
When I click the button in the top level view, it navigates correctly to the 2nd level view, displaying other items. But when the ViewModel is instanciated and the Command bound to the items in the listview, the Command is called once again, and again and again. My theory is that the DataTemplate notifies all of its parents (all instances) when binding the command. But maybe there is a bug somewhere in my code.
Is this behavior normal? and if it is, is there a way to do what I want to achieve in MVVM-respectful way?
Thank you in advance for your responses
The problem is right here:
Command="{Binding DataContext.ItemTouchCommand, RelativeSource={RelativeSource AncestorType={x:Type Control:ContentDisplay}}}"
Your Command is bound to the ContentDisplay's ViewModel. I would recommend taking your DataTemplate and making a UserControl with its own backing ViewModel (with the ItemTouchCommand on that ViewModel). Expose a dependency property on the UserControl that takes the type of the Item that is being displayed. Then you can change your datatemplate to simply hold an instance of that control like so
<DataTemplate x:Key="StandardContentDisplayDataTemplate">
<Control:ItemDisplayControl Item="{Binding}"/>
</DataTemplate>
精彩评论