FrameworkElement.Parent和VisualtreeHelper.GetParent的行为有所不同

编程入门 行业动态 更新时间:2024-10-25 20:30:30
本文介绍了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的行为有所不同

本文发布于:2023-11-05 15:51:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有所不同   Parent   FrameworkElement   GetParent   VisualtreeHelper

发布评论

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

>www.elefans.com

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