【问题标题】:why is the DataGrid MouseDoubleClick event fired when you double click on the scrollbar?为什么双击滚动条时会触发 DataGrid MouseDoubleClick 事件?
【发布时间】:2012-04-26 08:34:42
【问题描述】:

为什么双击滚动条或标题时会触发 DataGrid MouseDoubleClick 事件?

有什么办法可以避免这种情况,只有在我双击数据网格时才触发事件。

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    滚动条和标题是网格的一部分,但不处理双击,因此事件“冒泡”到网格。

    不优雅的解决方案是通过事件源或鼠标坐标来稍微找出“点击了什么”。

    但你也可以做类似的事情(未经测试):

    <DataGrid>
      <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
          <EventSetter Event="MouseDoubleClick" Handler="OnRowDoubleClicked"/>
        </Style>
      </DataGrid.RowStyle>
    </DataGrid>
    

    【讨论】:

    • 此解决方案仅在您要覆盖默认 DataGrid 组件时才有效 - 换句话说 - 基于默认组件创建自己的 DataGrid 组件。
    【解决方案2】:

    您可以在鼠标点击事件中查看点击点的详细信息 -

    DependencyObject dep = (DependencyObject)e.OriginalSource;
    
    // iteratively traverse the visual tree
    while ((dep != null) &&
            !(dep is DataGridCell) &&
            !(dep is DataGridColumnHeader))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }
    
    if (dep == null)
        return;
    
    if (dep is DataGridColumnHeader)
    {
        DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
        // do something
    }
    
    if (dep is DataGridCell)
    {
        DataGridCell cell = dep as DataGridCell;
        // do something
    }
    

    https://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题,用这个解决了:

      DependencyObject src = VisualTreeHelper.GetParent((DependencyObject)e.OriginalSource);
      if (!(src is Control) && src.GetType() != typeof(System.Windows.Controls.Primitives.Thumb))
      {
          //your code
      }
      

      我读了这篇文章来了解一下:How to detect double click on list view scroll bar?

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-14
        • 2023-01-06
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        • 2012-07-25
        相关资源
        最近更新 更多