How can you get the parent of a UIElement?

后端 未结 1 1595
梦毁少年i
梦毁少年i 2021-02-15 17:40

Ok, I know that FrameworkElement, which is a direct subclass of UIElement, has a Parent property, but Panels have children of type UIElement

相关标签:
1条回答
  • 2021-02-15 18:11

    Well, if you need to find a logical parent you can just use LogicalTreeHelper in the same manner as VisualTreeHelper.

    As for "...and have any thoughts why they didn't just define a Parent property there instead of on FrameworkElement?". Basically, the notion of "Logical Tree" is introduced on the FrameworkElement level. See http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx (see Remarks) for details of what FrameworkElement adds to UIElement. Here is what it says about Logical Trees:

    The logical tree: The general WPF programming model is often expressed in terms of being a tree of elements. Support for expressing the tree of elements as a logical tree, and accompanying support for defining that tree in markup is implemented at the FrameworkElement level. Note however that FrameworkElement deliberately does not define a content model, and leaves that responsibility to derived classes. For more information, see Trees in WPF.

    0 讨论(0)
提交回复
热议问题