AddHandler(DataGrid.MouseLeftButtonDownEvent, new RoutedEventHandler(grdStudyList_MouseLeftButtonDown), true);
2. 命中测试
private void grdStudyList_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{
Point CurrentPosition = System.Windows.Input.Mouse.GetPosition(grdStudyList);
_hitResultsList.Clear();
VisualTreeHelper.HitTest(grdStudyList, null,
new HitTestResultCallback(DataGridHitTestResult),
new PointHitTestParameters(CurrentPosition));
if (0 >= _hitResultsList.Count)
{
return;
}
var element = _hitResultsList.Where(y => y.GetType() == typeof(DataGridHeaderBorder)
|| y.GetType() == typeof(ScrollViewer));
bool isGridCell = true;
foreach (var x in element)
{
isGridCell = false;
}
if (!isGridCell)
{
return;
}
//后续逻辑处理
}
//
private List<DependencyObject> _hitResultsList = new List<DependencyObject>();
public HitTestResultBehavior DataGridHitTestResult(HitTestResult result)
{
// Set the behavior to return visuals at all z-order levels.
if (3 > _hitResultsList.Count)
{
_hitResultsList.Add(result.VisualHit);
return HitTestResultBehavior.Continue;
}
else
{
return HitTestResultBehavior.Stop;
}
}