using System.Collections.Generic; using System.Windows; using System.Windows.Media; namespace Artemis.UI.Shared { /// /// Provides utilities for running hit tests on visual elements /// public static class HitTestUtilities { /// /// Runs a hit test on children of the container within the rectangle matching all elements that have a data context of /// T /// /// /// /// /// public static List GetHitViewModels(Visual container, RectangleGeometry rectangleGeometry) { List result = new(); GeometryHitTestParameters hitTestParams = new(rectangleGeometry); HitTestResultBehavior ResultCallback(HitTestResult r) => HitTestResultBehavior.Continue; HitTestFilterBehavior FilterCallback(DependencyObject e) { if (e is FrameworkElement fe && fe.DataContext is T context && !result.Contains(context)) result.Add(context); return HitTestFilterBehavior.Continue; } VisualTreeHelper.HitTest(container, FilterCallback, ResultCallback, hitTestParams); return result; } } }