Просто создайте свое собственное свойство зависимостей DataGridDoubleClickProperty, к которому вы прикрепляете обработчик для события DataGrid.MouseDoubleClick
public static class Commands
{
public static readonly DependencyProperty DataGridDoubleClickProperty =
DependencyProperty.RegisterAttached("DataGridDoubleClickCommand", typeof ( ICommand ), typeof ( Commands ),
new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridDoubleClickEvent)));
public static ICommand GetDataGridDoubleClickCommand(DependencyObject obj)
{
return (ICommand) obj.GetValue(DataGridDoubleClickProperty);
}
public static void SetDataGridDoubleClickCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(DataGridDoubleClickProperty, value);
}
public static void AttachOrRemoveDataGridDoubleClickEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
DataGrid dataGrid = obj as DataGrid;
if ( dataGrid != null )
{
ICommand cmd = (ICommand) args.NewValue;
if ( args.OldValue == null && args.NewValue != null )
{
dataGrid.MouseDoubleClick += ExecuteDataGridDoubleClick;
}
else if ( args.OldValue != null && args.NewValue == null )
{
dataGrid.MouseDoubleClick -= ExecuteDataGridDoubleClick;
}
}
}
private static void ExecuteDataGridDoubleClick(object sender, MouseButtonEventArgs args)
{
DependencyObject obj = sender as DependencyObject;
ICommand cmd = (ICommand) obj.GetValue(DataGridDoubleClickProperty);
if ( cmd != null )
{
if ( cmd.CanExecute(obj) )
{
cmd.Execute(obj);
}
}
}
}
В своем представлении вы используете Binding для сопоставления этого свойства DependencyProperty с командой
<Grid DataContext="{StaticResource viewModel}">
<DataGrid AutoGenerateColumns="True"
ItemsSource="{Binding Data}"
SelectedItem="{Binding SelectedItem}"
clr:Commands.DataGridDoubleClickCommand="{Binding DataGridDoubleClick}"
/>
</Grid>
DataGridDoubleClickтакое свойство ICommand в вашем классе ViewModel