如何添加鼠标双击数据网格项目wpf

Mam*_*d R 2 wpf

在Windows窗体应用程序中,我们的数据网格视图有许多事件,如行鼠标双击或行单击和额外...

但在WPF我找不到这些事件.如何添加行鼠标双击到我的用户控件,其中包含数据网格

我做了一些不好的方式,我使用数据网格鼠标双击事件和一些错误发生在这种方式,但我想知道简单和标准的方式

我还在row_load事件中向数据网格项添加了双击事件,但是如果数据网格有很大的来源,它似乎会使我的程序变慢

private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
    e.Row.MouseDoubleClick += new MouseButtonEventHandler(Row_MouseDoubleClick);
}
Run Code Online (Sandbox Code Playgroud)

Col*_*inE 7

您可以处理双击DataGrid元素,然后查看事件源以查找单击的行和列:

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject dep = (DependencyObject)e.OriginalSource;

    // iteratively traverse the visual tree
    while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    if (dep is DataGridColumnHeader)
    {
        DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
        // do something
    }

    if (dep is DataGridCell)
    {
        DataGridCell cell = dep as DataGridCell;
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这篇写的博文中详细描述了这一点.