FrameworkElement.Parent and VisualtreeHelper.GetParent behaves differently

本秂侑毒 提交于 2019-12-01 21:07:21

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

<ControlA>
   <ControlB />
</ControlA>

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

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.

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.

"The value reported by the FrameworkElement.Parent property in Silverlight is generally the same value that would be reported by using VisualTreeHelper API, because the two tree concepts are frequently synchronized as far as how parent-child relationships are reported. However, there may be cases where FrameworkElement.Parent reports a parent that VisualTreeHelper does not."

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.parent(v=VS.95).aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!