wpf - 取消选择 wpf 数据网格中的行

标签 wpf wpf-controls binding wpfdatagrid double-click

我有

<DataGrid Name="grid" MouseDoubleClick="Attributes_MouseDoubleClick" >

每当单击事件发生在 Datagrid 行以外的任何地方时,我都需要取消选择行

grid.CurrentItem 应该是 null

我需要连续触发双击事件。 但是,问题是,一旦我选择一行并双击网格上的其他地方(标题、空滚动查看器区域等),双击事件就会按预期触发,但 CurrentItem 有时是选定的行,有时是空的。

为了防止这种行为..我需要取消选择所选行。

关于我应该如何处理这个问题有什么想法吗?

谢谢。

最佳答案

您可以在事件源的可视化树中搜索 DataGridRow 类型的实例,以确定您双击的是一行还是其他地方。

以下网址Detecting Double Click Events on the WPF DataGrid包含很好的例子。
我已在此处包含代码,以防该站点不再可用。

这是双击的事件处理程序:

private void DataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  //search the object hierarchy for a datagrid row
  DependencyObject source = (DependencyObject)e.OriginalSource;
  var row = DataGridTextBox.Helpers.UIHelpers.TryFindParent<DataGridRow>(source);

  //the user did not click on a row
  if (row == null) return;

  //[insert great code here...]

  e.Handled = true;
}

下面是帮助搜索可视化树的代码:

using System.Windows;
using System.Windows.Media;

namespace DataGridTextBox.Helpers
{
  public static class UIHelpers
  {
    public static T TryFindParent<T>(this DependencyObject child) where T : DependencyObject
    {
      //get parent item
      DependencyObject parentObject = GetParentObject(child);

      //we've reached the end of the tree
      if (parentObject == null) return null;

      //check if the parent matches the type we're looking for
      T parent = parentObject as T;
      if (parent != null)
      {
        return parent;
      }
      else
      {
        //use recursion to proceed with next level
        return TryFindParent<T>(parentObject);
      }
    }

    public static DependencyObject GetParentObject(this DependencyObject child)
    {
      if (child == null) return null;

      //handle content elements separately
      ContentElement contentElement = child as ContentElement;
      if (contentElement != null)
      {
        DependencyObject parent = ContentOperations.GetParent(contentElement);
        if (parent != null) return parent;

        FrameworkContentElement fce = contentElement as FrameworkContentElement;
        return fce != null ? fce.Parent : null;
      }

      //also try searching for parent in framework elements (such as DockPanel, etc)
      FrameworkElement frameworkElement = child as FrameworkElement;
      if (frameworkElement != null)
      {
        DependencyObject parent = frameworkElement.Parent;
        if (parent != null) return parent;
      }

      //if it's not a ContentElement/FrameworkElement, rely on VisualTreeHelper
      return VisualTreeHelper.GetParent(child);
    }
  }
}

关于wpf - 取消选择 wpf 数据网格中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4815436/

相关文章:

wpf - 在 WPF 应用程序中做广告?

wpf - 哪个重量最轻 : Canvas or Grid?

wpf - Windows 讲述人读取窗口中所有控件的名称(甚至是隐藏的控件)

c# - 为 MenuItem 使用 DisplayMemeberBinding 的多重绑定(bind)

wpf - StringFormat 中的空格

c# - DynamicResources 无法加载到以编程方式创建的控件中

c# - 显示来自 byte[ ] 的图像

c# - WPF 图表工具包 - 更改区域系列颜色的不透明度

ruby - 如何在 Ruby 中创建/提取变量/哈希到当前绑定(bind)中?

c# - 支持对象删除状态的 DTO 的 BindingList