【发布时间】:2012-04-26 08:34:42
【问题描述】:
为什么双击滚动条或标题时会触发 DataGrid MouseDoubleClick 事件?
有什么办法可以避免这种情况,只有在我双击数据网格时才触发事件。
【问题讨论】:
为什么双击滚动条或标题时会触发 DataGrid MouseDoubleClick 事件?
有什么办法可以避免这种情况,只有在我双击数据网格时才触发事件。
【问题讨论】:
滚动条和标题是网格的一部分,但不处理双击,因此事件“冒泡”到网格。
不优雅的解决方案是通过事件源或鼠标坐标来稍微找出“点击了什么”。
但你也可以做类似的事情(未经测试):
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="OnRowDoubleClicked"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
【讨论】:
您可以在鼠标点击事件中查看点击点的详细信息 -
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
【讨论】:
我也遇到了同样的问题,用这个解决了:
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?
希望对你有帮助:)
【讨论】: