【问题标题】:How can i add mouse double click to data grid items wpf如何将鼠标双击添加到数据网格项目 wpf
【发布时间】:2012-05-05 05:18:05
【问题描述】:

在 windows 窗体应用程序中,我们的数据网格视图有很多事件,例如行鼠标双击或行单击等...

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

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

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

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

【问题讨论】:

    标签: wpf


    【解决方案1】:

    您可以处理双击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中详细描述了这一点。

    【讨论】:

      【解决方案2】:

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

      我想知道什么时候点击了所有行,我使用了这个

      【讨论】:

        猜你喜欢
        • 2020-06-22
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 2023-03-08
        • 2014-11-05
        • 1970-01-01
        • 1970-01-01
        • 2013-09-21
        相关资源
        最近更新 更多