爲什麼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