问题与WPF中的VisualTreeHelper.HitTest(Problem with VisualTreeHelper.HitTest in WPF)

编程入门 行业动态 更新时间:2024-10-25 18:24:09
问题与WPF中的VisualTreeHelper.HitTest(Problem with VisualTreeHelper.HitTest in WPF)

我试图在Canvas上测试一堆UserControl。 我不希望HitTest()遍历整个可视化树,所以我使用FilterCallback来确保我只是命中测试UserControl。

我的问题是,用户控件永远不会打,它应该,但它不会。 如果我使用FilterCallback,则返回它没有任何结果。 如果让HitTest在可视化树中运行,它将跳过UserControl。

以下是一些代码:

<Canvas x:Name="Container"> <UserControl> <Grid> <Rectangle /> </Grid> </UserControl> <UserControl> <Grid> <Rectangle /> </Grid> </UserControl> </Canvas> ... VisualTreeHelper.HitTest(Container, OnFilter, OnResult, myPoint); ... private void OnResult(DependencyObject o) { //I'll get the Rectangle here, but never the userControl } private void OnFilter(DependencyObject o) { //I will get the UserControl here, but even when I do nothing more than continue, it will not trigger a visualHit. But the child rectangle will. }

I'm trying to hit-test a bunch of UserControls on a Canvas. I don't want the HitTest() to walk the whole way through the visual tree, so I'm using the FilterCallback to make sure I only hit-test the UserControl.

My problem is that the UserControl never hits, it should, but it doesn't. If I use the FilterCallback, I return that it hit nothing. If I let the HitTest run through the visual tree, it skips the UserControl.

Here's some code:

<Canvas x:Name="Container"> <UserControl> <Grid> <Rectangle /> </Grid> </UserControl> <UserControl> <Grid> <Rectangle /> </Grid> </UserControl> </Canvas> ... VisualTreeHelper.HitTest(Container, OnFilter, OnResult, myPoint); ... private void OnResult(DependencyObject o) { //I'll get the Rectangle here, but never the userControl } private void OnFilter(DependencyObject o) { //I will get the UserControl here, but even when I do nothing more than continue, it will not trigger a visualHit. But the child rectangle will. }

最满意答案

我有同样的HitTest问题找不到用户控件。 显然这是由设计( http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/005dad03-c8eb-405f-9567-50653a0e612c )。

我通过处理用户控件中某些元素的命中,然后使用VisualTreeHelper.GetParent方法查找父级用户控件来解决此问题。 我对WPF还不是很熟悉,所以我不确定使用FrameworkElement.Parent属性会更好。

但是,下面是我的方法,用于在通过命中测试首次查找其某些内容元素后,查找用户控件(或某些所需类型的任何可视父项):

public static T GetVisualParent<T>(this DependencyObject element) where T : DependencyObject { while (element != null && !(element is T)) element = VisualTreeHelper.GetParent(element); return (T)element; }

I had this same problem of HitTest not finding a user control. Apparently this is by design (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/005dad03-c8eb-405f-9567-50653a0e612c).

I worked around this by handling the hit of some element inside the user control, and then finding the parent user control using the VisualTreeHelper.GetParent method. I'm not very familiar with WPF yet, so I'm not sure if it would be better to use FrameworkElement.Parent property.

However, here is my method for finding the user control (or any visual parent of some required type) after first finding some of its content elements by hit test:

public static T GetVisualParent<T>(this DependencyObject element) where T : DependencyObject { while (element != null && !(element is T)) element = VisualTreeHelper.GetParent(element); return (T)element; }

更多推荐

本文发布于:2023-08-05 15:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1433953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:VisualTreeHelper   WPF   Problem   HitTest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!