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

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

问题描述

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

我需要的取消选择的行只要点击事件发生比DataGrid行等其他任何地方。

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

即。 grid.CurrentItem

我要火双击事件的上的一行。
但是,问题是,一旦我选择一行,对电网(头,空的ScrollViewer区等)的其他地方双击双击预期事件触发,但CURRENTITEM有时选定行,有时空。

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.

要prevent这种行为。我可以不选选中的行。

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

任何想法,我应该如何处理呢?

Any ideas as to how I should approach this?

感谢。

推荐答案

您可以搜索事件源的视觉树的类型的实例DataGridRow以确定如果双击点击行或其他地方。

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.

以下网站检测双击事件在WPF的DataGrid 包含很好的例子。结果
我已经包括这里的code的情况下,该网站不再提供。

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

下面是code,以帮助搜索Visual树:

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天全站免登陆