如何获得UIElement的父级

如何获得UIElement的父级

本文介绍了如何获得UIElement的父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道FrameworkElementUIElement的直接子类,它具有Parent属性,但是Panel的子类型为UIElement而不是FrameworkElement(子类型为UIElementCollection)这似乎意味着您可以将UIElement直接添加到面板中.

Ok, I know that FrameworkElement, which is a direct subclass of UIElement, has a Parent property, but Panels have children of type UIElement, not FrameworkElement (Children is of type UIElementCollection) which seems it would mean you can add UIElements directly to a Panel.

也就是说,如果您有一个UIElement并想查看它是否具有作为面板的父级,那么我知道如何测试它的唯一方法是使用VisualTreeHelper,但这是可视化树,而不是逻辑树. (至少我们知道面板没有模板,所以也许是这样,但是仍然...)

That said, if you have a UIElement and want to see if it has a parent that's a panel, the only way I know how to test this is with the VisualTreeHelper, but that's the visual tree, not the logical tree. (At least we know a Panel doesn't have a template so maybe that's the way, but still...)

那么从VisualTreeHelper出发,有人知道如何知道UIElement的父面板吗? (...还有什么想法为什么他们不只是在此处而不是在FrameworkElement上定义Parent属性?)

So asides from the VisualTreeHelper, does anyone know how to know which panel is the parent of a UIElement? (...and have any thoughts why they didn't just define a Parent property there instead of on FrameworkElement?)

谢谢!

推荐答案

好吧,如果您需要查找逻辑父对象,则可以像VisualTreeHelper一样使用LogicalTreeHelper.

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

至于"...还有什么想法,为什么他们不只是在FrameworkElement上而不是在此处定义Parent属性?".基本上,在FrameworkElement级别上引入了逻辑树"的概念.请参阅 http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx (请参见备注"),以获取FrameworkElement添加到UIElement的详细信息.这是有关逻辑树的内容:

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:

这篇关于如何获得UIElement的父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:09