Search code examples
c#wpfxamlwpfdatagrid

Issue with doubleclick on datagrid


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);
}

Solution

  • 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();
    }