取消选择 wpf 数据网格中的行 [英] unselect row in wpf datagrid

查看:27
本文介绍了取消选择 wpf 数据网格中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

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

grid.CurrentItem 应该是 null

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

为了防止这种行为..我需要取消选择选定的行.

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

谢谢.

解决方案

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

以下站点检测双击WPF DataGrid 上的事件 包含很好的示例.
我在此处包含了代码,以防该站点不再可用.

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

private void DataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e){//在对象层次结构中搜索数据网格行DependencyObject source = (DependencyObject)e.OriginalSource;var row = DataGridTextBox.Helpers.UIHelpers.TryFindParent(来源);//用户没有点击一行如果(行==空)返回;//[在此处插入很棒的代码...]e.handled = true;}

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

使用 System.Windows;使用 System.Windows.Media;命名空间 DataGridTextBox.Helpers{公共静态类 UIHelpers{public static T TryFindParent(this DependencyObject child) where T : DependencyObject{//获取父项DependencyObject parentObject = GetParentObject(child);//我们已经到达树的末端if (parentObject == null) 返回 null;//检查父类是否匹配我们正在寻找的类型T parent = parentObject as T;如果(父母!= null){返回父母;}别的{//使用递归进行下一层返回 TryFindParent(parentObject);}}public static DependencyObject GetParentObject(this DependencyObject child){if (child == null) 返回 null;//单独处理内容元素ContentElement contentElement = child as ContentElement;如果(内容元素!= null){DependencyObject parent = ContentOperations.GetParent(contentElement);如果(父级!= null)返回父级;FrameworkContentElement fce = contentElement 作为 FrameworkContentElement;返回 fce != null ?fce.Parent:空;}//也尝试在框架元素中搜索父元素(例如 DockPanel 等)FrameworkElement frameworkElement = child as FrameworkElement;如果(框架元素!= null){DependencyObject parent = frameworkElement.Parent;如果(父级!= null)返回父级;}//如果不是ContentElement/FrameworkElement,则依赖VisualTreeHelper返回 VisualTreeHelper.GetParent(child);}}}

I have

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

I need to unselect a row whenever a click event occurs anywhere else other than the Datagrid row.

i.e. grid.CurrentItem should be null

I need to fire a double-click event only on a row. But, the problem is, once I select a row and double-click elsewhere on the grid(header, empty scrollviewer area, etc) the double-click event fires as expected but the CurrentItem is sometimes the selected row and sometimes null.

To prevent this behaviour.. I need to unselect the selected row.

Any ideas as to how I should approach this?

Thanks.

解决方案

You can search the Visual Tree of the event source for an instance of type DataGridRow to determine if you double clicked on a row or somewhere else.

The following site Detecting Double Click Events on the WPF DataGrid contains good example.
I've include the code here in case the site is no longer available.

Here is the event handler for double click:

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

Here is the code to help search the Visual Tree:

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 数据网格中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆