How do i handle cell double click event on WPF DataGrid, equivalent to windows DataGrid's Events?

后端 未结 3 1971
旧时难觅i
旧时难觅i 2020-12-16 02:08

As you know, in windows C#\'s gridview, if we want to handle a click/double click event on cell then there are events like CellClick, CellDoubleClick, etc.

So, i wan

3条回答
  •  误落风尘
    2020-12-16 02:29

    I know coding WPF is sometimes a PITA. Here you would have to handle the MouseDoubleClick event anyway. Then search the source object hierarchy to find a DataGridRow and do whatever with it.

    UPDATE: Sample code

    XAML

    
    

    Code behind

    private void OnDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DependencyObject source = (DependencyObject) e.OriginalSource;
        var row = GetDataGridRowObject(source);
        if (row == null)
        {
             return;
        }
        else
        {
            // Do whatever with it
        }
        e.Handled = true;
    }
    
    private DataGridRow GetDataGridRowObject(DependencyObject source)                               
    {
        // Write your own code to recursively traverse up via the source
        // until you find a DataGridRow object. Otherwise return null.
    }
    

    }

提交回复
热议问题