首页 文章

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

提问于
浏览
2

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

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

我做了一些不好的方式,我用 data grid mouse double click event 和一些错误发生在这种方式 but i want know simple and standard way

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

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

2 回答

  • 0

    您可以处理双击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
        }
    }
    

    我在this blog post that I wrote中详细描述了这一点 .

  • 7

    Colin的答案非常好并且有效......我也使用这段代码,这对我很有用,并希望与其他人分享 .

    private void myGridView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                DependencyObject dep = (DependencyObject)e.OriginalSource;
    
                // iteratively traverse the visual tree
                while ((dep != null) && !(dep is DataGridRow) && !(dep is DataGridColumnHeader))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }
    
                if (dep == null)
                    return;
    
                if (dep is DataGridRow)
                {
                    DataGridRow row = dep as DataGridRow;
                   //here i can cast the row to that class i want 
                }
            }
    

    因为我想知道当所有行点击我使用这个

相关问题