No need for attached behaviors or custom DataGrid subclasses here.
In your DataGrid
, bind ItemsSource
to an ICollectionView
. The trick here is to set IsSynchronizedWithCurrentItem="True"
which means the selected row will be the current item.
The second part of the trick is to bind CommandParameter
to the current item with the forward slash syntax.
When a row is double clicked, the command will be executed with the clicked row as argument.
<DataGrid
ItemsSource="{Binding CollectionView}"
IsSynchronizedWithCurrentItem="True">
<DataGrid.InputBindings>
<MouseBinding
MouseAction="LeftDoubleClick"
Command="{Binding DoubleClickCommand}"
CommandParameter="{Binding CollectionView/}"/>
</DataGrid.InputBindings>
</DataGrid>
This is how a (simplified) version of the view model would look:
class MyViewModel
{
public ICollectionView CollectionView { get; set; }
public ICommand DoubleClickCommand { get; set; }
}