C# 什么是Silverlight';s FindElementsInHost坐标是否等效于WPF?
我想在WPF画布组件上执行一个矩形命中测试,以获得被矩形框架元素重叠的控件。我找到了Silverlight的C# 什么是Silverlight';s FindElementsInHost坐标是否等效于WPF?,c#,wpf,silverlight,hittest,visualtreehelper,C#,Wpf,Silverlight,Hittest,Visualtreehelper,我想在WPF画布组件上执行一个矩形命中测试,以获得被矩形框架元素重叠的控件。我找到了Silverlight的VisualTreeHelper.FindElementsInHostCoordinates方法,但显然它在WPF中不可用 实现这种功能的最佳方法是什么?最接近的等效方法是。它的工作原理与Silverlight的FindElementsInHostCoordinates有很大不同,但您应该能够根据自己的需要使用它。假设您在Silverlight中有这样的调用 var result = Vi
VisualTreeHelper.FindElementsInHostCoordinates
方法,但显然它在WPF中不可用
实现这种功能的最佳方法是什么?最接近的等效方法是。它的工作原理与Silverlight的FindElementsInHostCoordinates有很大不同,但您应该能够根据自己的需要使用它。假设您在Silverlight中有这样的调用
var result = VisualTreeHelper.FindElementsInHostCoordinates(myPoint, myUIElement);
那么这个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));
var result=newlist();
//已从外部编辑更改,因为VisualHit是
//只能是DependencyObject,不能是UIElement
//这可能会导致异常或根本无法编译
//只需过滤UIElement类的结果,然后
//如果需要,将其转换为IEnumerable
//完全相同的结果,包括类型
VisualTreeHelper.HitTest(
myUiElement,
无效的
新HittTestResultCallback(
(HitTestResult命中)=>{
结果.添加(hit.VisualHit);
返回HitTestResultBehavior。是否继续;
}),
新的PointHitTestParameters(myPoint));
在您的特殊情况下,您可能希望使用GeometryHitTestParameters
而不是PointHitTestParameters
来执行Rect测试