FrameworkElement.Parent和VisualtreeHelper.GetParent的行为有所不同 [英] FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently

查看:154
本文介绍了FrameworkElement.Parent和VisualtreeHelper.GetParent的行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WP7应用程序中,我递归地使用FrameworkElement.Parent来确定特定元素是否在另一个元素内部。但是它并不总是能正常工作。
然后我将代码更改为递归使用VisualtreeHelper.GetParent方法,并且它始终可以正常工作(据我测试)。

In a WP7 app, I used FrameworkElement.Parent recursively to determine whether a specific element is inside of another element. But it does not always work correctly. Then I changed my code to use VisualtreeHelper.GetParent method recursively, and it always work correctly(so far as I tested).

那么有什么区别

谢谢

推荐答案

考虑 ControlA ControlB ,其中ControlA是内容控件,表示它可以包含其他控件。

Consider ControlA and ControlB where ControlA is a content control meaning it can contain other controls.

<ControlA>
   <ControlB />
</ControlA>

在这种情况下,ControlB实例的Parent属性是ControlA的实例。

Logically the Parent property of the ControlB instance is an instance of ControlA in this case.

但是,如果您要查看ControlA默认样式的 Template 设置器,您可能会发现复杂的Xaml,其中包含一些视觉状态管理和边界和网格的层次结构,最后埋在该地段中,这是一个放置子控件的 ContentPresenter

However if you were to look at Template setter of the default style of ControlA you may find complex Xaml containing some visual state management and a hierarchy of Borders and Grids and finally buried in that lot there is a ContentPresenter where the child control will be placed.

递归循环VisualTreeHelper。Parent将找到模板中定义的每个元素,从 ContentPresenter 到逻辑父元素,然后到其他。

Recursively looping up VisualTreeHelper.Parent will find each element defined in the template from the ContentPresenter up to the "logical" parent element and then beyond.

这篇关于FrameworkElement.Parent和VisualtreeHelper.GetParent的行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆