【问题标题】:How to acess DataGridCell on Right click on WPF DataGrid如何在 WPF DataGrid 上右键单击访问 DataGrid 单元格
【发布时间】:2012-11-19 07:33:24
【问题描述】:

我有一个WPFDataGrid 并有一个事件MouseRightButtonUp 用于右键单击DataGrid。如何在事件处理程序中访问DataGridCell

private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
      //access DataGridCell on which mouse is right clicked
      //Want to access cell here
}

【问题讨论】:

    标签: c# wpf datagrid wpfdatagrid right-click


    【解决方案1】:

    出于某种原因,我从来没有真正喜欢使用可视化树助手,但在这种情况下可以使用它。

    基本上它的作用是在单击右键时对鼠标下的控件进行点击测试,并使用可视化树帮助器类向上导航可视化树,直到您点击一个单元格对象。

    private void DataGrid_MouseRightButtonUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        var hit = VisualTreeHelper.HitTest((Visual)sender, e.GetPosition((IInputElement)sender));
        DependencyObject cell = VisualTreeHelper.GetParent(hit.VisualHit);
        while (cell != null && !(cell is System.Windows.Controls.DataGridCell)) cell = VisualTreeHelper.GetParent(cell);
        System.Windows.Controls.DataGridCell targetCell = cell as System.Windows.Controls.DataGridCell;
    
        // At this point targetCell should be the cell that was clicked or null if something went wrong.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多