6

I have the following on a datagrid in my C# code:

<DataGrid.InputBindings>  
    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding CmdTransUnitFillerRowDblClick}" />  
</DataGrid.InputBindings>  

It works for the most part except if user first selects the row (single click) and then tries double-clicking the row. In this situation the CmdTransUnitFillerRowDblClick code is never fired for processing.

So, how can I get the CmdTransUnitFillerRowDblClick to fire correctly on a double-click when the row is already selected?
Since someone may ask:

private void ExecutecmdTransUnitFillerRowDblClick(object parameter)  
{
    if (DgTransUnitFillerSelectedItem != null)
        TransUnitFillerDoubleClick(DgTransUnitFillerSelectedItem.CollectionRowId);
}

2 Answers 2

2
+50

See my answer to another related question. The problem is that the datagrid no longer has the focus after the user selects a row (or cell, actually); the cell that the user clicked in the datagrid does. So you have to change the focus back to the datagrid to allow this.

Change:

<DataGrid.InputBindings>  
    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding CmdTransUnitFillerRowDblClick}" />  
</DataGrid.InputBindings>

To:

<DataGrid.InputBindings>  
    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding CmdTransUnitFillerRowDblClick}" />  
    <MouseBinding Gesture="LeftClick" Command="{Binding CmdTransUnitFillerRowClick}" />  
</DataGrid.InputBindings>

...and add:

private void ExecutecmdTransUnitFillerRowClick(object parameter)  
{
    if (DgTransUnitFillerSelectedItem != null)
        The_Name_Of_Your_DataGrid.Focus();
}
6
  • Untested, but couldn't he just listen to the DoubleClick event anyway and ignore e.Handled? Jun 30, 2014 at 20:07
  • @TroelsLarsen The DoubleClick event on the DataGrid will never fire if the cell keeps the focus, so he has to either add another DoubleClick event on the cell or change the focus.
    – klugerama
    Jun 30, 2014 at 20:13
  • Ah.. I just saw that it was an InputBinding, not an event. :) Jun 30, 2014 at 20:42
  • Thanks, makes sense after the explanation. Seems to be working for me Jul 1, 2014 at 16:40
  • 1
    @klugerama, how can you do this using the MVVM format? Jul 19, 2018 at 15:54
0

On top of your existing InputBinding, you can use a Style to attach the InputBinding to each cell:

<Style TargetType="DataGridCell">
    <Setter Property="local:MouseCommands.LeftDoubleClick" Value="ApplicationCommands.New" />
</Style>

This requires use of the MouseCommands class from here.

public static class MouseCommands
{
    private static void LeftDoubleClickChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var control = (Control)sender;

        if (args.NewValue != null && args.NewValue is ICommand)
        {
            var newBinding = new MouseBinding(args.NewValue as ICommand, new MouseGesture(MouseAction.LeftDoubleClick));
            control.InputBindings.Add(newBinding);
        }
        else
        {
            var oldBinding = control.InputBindings.Cast<InputBinding>().First(b => b.Command.Equals(args.OldValue));
            control.InputBindings.Remove(oldBinding);
        }
    }

    public static readonly DependencyProperty LeftDoubleClickProperty =
        DependencyProperty.RegisterAttached("LeftDoubleClick",
            typeof(ICommand),
            typeof(MouseCommands),
            new UIPropertyMetadata(LeftDoubleClickChanged));

    public static void SetLeftDoubleClick(DependencyObject obj, ICommand value)
    {
        obj.SetValue(LeftDoubleClickProperty, value);
    }

    public static ICommand GetLeftDoubleClick(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(LeftDoubleClickProperty);
    }
}

Though I think a cleaner way is to just handle the MouseDoubleClick event in the code-behind and manually raise the Command execution by calling your ViewModel directly, or calling .Execute() on the command.

2
  • I thought of going down the code-behind route but this was something setup by someone else and they didn't want to use code-behind so I have been attempting to keep that philosophy while attempting to fix this issue. I will give your solution a try. Jul 1, 2014 at 14:13
  • This may work in some situations for me but with some interfaces we were using on the datagrid it was tossing errors like crazy. Saved the link to my favorites so I can reference in the future as needed Jul 1, 2014 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.