为什么DataGrid的MouseDoubleClick时触发的事件我双击滚动条或头为什么双击滚动条时会触发DataGrid MouseDoubleClick事件?
有什么办法只对事件避免这种ANF火当我点击数据网格里面的双
为什么DataGrid的MouseDoubleClick时触发的事件我双击滚动条或头为什么双击滚动条时会触发DataGrid MouseDoubleClick事件?
有什么办法只对事件避免这种ANF火当我点击数据网格里面的双
一个滚动条nd标题是网格的一部分,但不处理双击,所以事件会“起泡”到网格。
这个不雅的解决方案是通过事件源或鼠标坐标的平均值稍微发现“被点击的东西”。
但你也可以做这样的事情(未经测试):
<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
}
更多信息: http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/
我与此有同样的问题,并解决它:
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?
我希望这会帮助:)
非常感谢它工作得很好 – 2012-04-26 09:46:55