【问题标题】:unselect row in wpf datagrid取消选择 wpf 数据网格中的行
【发布时间】:2011-06-16 11:18:51
【问题描述】:

我有

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

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

grid.CurrentItem 应该是 null

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

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

关于我应该如何处理这个问题的任何想法?

谢谢。

【问题讨论】:

    标签: wpf wpf-controls binding wpfdatagrid double-click


    【解决方案1】:

    您可以在事件源的可视树中搜索 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);
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      该事件由网格触发,而不是由行触发。我的意思是,当您双击网格中的某个位置时触发,它可能是选中的行(或者不是您认为更糟的行)。

      您可以编辑每一行的 itemtemplate 并在其中包含您的双击事件或capture 鼠标的坐标,但这有点棘手。

      希望有所帮助!

      【讨论】:

      猜你喜欢
      • 2011-11-20
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多