在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的行为有所不同
发布评论