4

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?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

4 Answers4

9
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>
vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
  • will it pass which row is clicked somehow? – Oleg Vazhnev Nov 30 '11 at 18:18
  • if you add a MySelectedItem to your ViewModel and use TwoWay binding, your viewmodel will be aware of the selected item. so kinda but it's better IMO – vidalsasoon Nov 30 '11 at 18:26
  • hm...... can I use this with C# project? because VS suggest me to reference some Visual Basic files – Oleg Vazhnev Nov 30 '11 at 18:37
  • 2
    you need to add the following reference: System.Windows.Interactivity. Yes this works with C#. – vidalsasoon Nov 30 '11 at 18:59
  • thanks, it works. may you also help me to do this? "if you add a MySelectedItem to your ViewModel and use TwoWay binding, your viewmodel will be aware of the selected item. so kinda but it's better IMO" – Oleg Vazhnev Nov 30 '11 at 19:35
  • i will try to do this myself using this question http://stackoverflow.com/questions/3913580/get-selected-row-item-in-datagrid-wpf ... – Oleg Vazhnev Nov 30 '11 at 19:38
  • I keep adding the dll and posting that exact code into my XAML but It still can't find Interaction.Triggers – SoftwareSavant Jun 19 '13 at 18:34
3

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>
Rachel
  • 130,264
  • 66
  • 304
  • 490
2

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).

Bas
  • 26,772
  • 8
  • 53
  • 86
  • i'm not sure I would like to add framework. my application is pretty small and so far it's fine without framework. how to do what I want to do without framework? – Oleg Vazhnev Nov 30 '11 at 14:15
0

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.

SvenG
  • 5,155
  • 2
  • 27
  • 36