I would like to perform a rectangual hit test on WPF Canvas component in order to get Controls that are overlapped by a Rectangle framework element. I found a Silverlight's VisualTreeHelper.FindElementsInHostCoordinates
method, but apparently it's not available in WPF.
我想对WPF Canvas组件执行rectangual命中测试,以获得与Rectangle框架元素重叠的控件。我找到了Silverlight的VisualTreeHelper.FindElementsInHostCoordinates方法,但显然它在WPF中不可用。
What's the best method to achieve such functionality?
实现此类功能的最佳方法是什么?
2 个解决方案
#1
3
The closest equivalent is VisualTreeHelper.HitTest. It works significantly differently to Silverlight's FindElementsInHostCoordinates
, but you should be able to use it for your needs.
最接近的等价物是VisualTreeHelper.HitTest。它的工作方式与Silverlight的FindElementsInHostCoordinates有很大不同,但您应该能够根据自己的需要使用它。
#2
3
Supposing you have a call like this in Silverlight
假设您在Silverlight中有这样的调用
var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement);
then this WPF code should have an equivalent result
那么这个WPF代码应该有相同的结果
var result = new List<DependencyObject>();
//changed from external edits, because VisualHit is
//only a DependencyObject and may not be a UIElement
//this could cause exceptions or may not be compiling at all
//simply filter the result for class UIElement and
//cast it to IEnumerable<UIElement> if you need
//the very exact same result including type
VisualTreeHelper.HitTest(
myUiElement,
null,
new HitTestResultCallback(
(HitTestResult hit)=>{
result.Add(hit.VisualHit);
return HitTestResultBehavior.Continue;
}),
new PointHitTestParameters(myPoint));
in your special case you might want to use GeometryHitTestParameters
instead of PointHitTestParameters
to do a Rect-Test.
在您的特殊情况下,您可能希望使用GeometryHitTestParameters而不是PointHitTestParameters来执行Rect-Test。
#1
3
The closest equivalent is VisualTreeHelper.HitTest. It works significantly differently to Silverlight's FindElementsInHostCoordinates
, but you should be able to use it for your needs.
最接近的等价物是VisualTreeHelper.HitTest。它的工作方式与Silverlight的FindElementsInHostCoordinates有很大不同,但您应该能够根据自己的需要使用它。
#2
3
Supposing you have a call like this in Silverlight
假设您在Silverlight中有这样的调用
var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement);
then this WPF code should have an equivalent result
那么这个WPF代码应该有相同的结果
var result = new List<DependencyObject>();
//changed from external edits, because VisualHit is
//only a DependencyObject and may not be a UIElement
//this could cause exceptions or may not be compiling at all
//simply filter the result for class UIElement and
//cast it to IEnumerable<UIElement> if you need
//the very exact same result including type
VisualTreeHelper.HitTest(
myUiElement,
null,
new HitTestResultCallback(
(HitTestResult hit)=>{
result.Add(hit.VisualHit);
return HitTestResultBehavior.Continue;
}),
new PointHitTestParameters(myPoint));
in your special case you might want to use GeometryHitTestParameters
instead of PointHitTestParameters
to do a Rect-Test.
在您的特殊情况下,您可能希望使用GeometryHitTestParameters而不是PointHitTestParameters来执行Rect-Test。