Reputation: 24067
I want a command in my ViewModel to be executed when DataGrid item is clicked. As a parameter I want to have corresponding row.
I've found one approach in internet but it using DependencyProperty
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/632ea875-a5b8-4d47-85b3-b30f28e0b827
I don't use DependencyProperty
in my project, instead i'm using INotifyPropertyChanged
. How to implement "double click in datagrid" commaind without using DependencyProperty
?
Upvotes: 4
Views: 8063
Reputation: 4401
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
...
<DataGrid SelectedItem={Binding MySelectedItem, Mode=TwoWay}>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding YourCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
Upvotes: 9
Reputation: 132558
I usually use an AttachedCommandBehavior. It's 3 class files which can be added to your project, and lets you attach commands to just about any event.
Here's an example of how it can be used:
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="local:CommandBehavior.Event" Value="MouseDoubleClick" />
<Setter Property="local:CommandBehavior.Action" Value="{Binding MyDoubleClickCommand}" />
<Setter Property="local:CommandBehavior.CommandParameter" Value="{Binding }" />
</Style>
Upvotes: 3
Reputation: 5195
You can use this code-behind snippet to identify the row that is double clicked.
In the line with the comment "//do something with the underlying data" you can get the attached ViewModel from the Grid's or row's DataContext and Invoke your Command with the row as parameter.
Upvotes: 0
Reputation: 27095
The MVVM Light Toolkit provides EventToCommand behavior, this should be able to achieve the desired behavior (you can always roll your own if you don't want to use the framework).
Upvotes: 2