简体   繁体   English

WPF FrameworkElement父级和移动UIElement

[英]WPF FrameworkElement Parent and Moving a UIElement

I'm trying to move a control from one parent to another (if this will work I'm not quite sure). 我正在尝试将控件从一个父母移动到另一个父母(如果这样可行,我不太确定)。 I can get a hold of the control that I want to move. 我可以掌握我想要移动的控件。 Here is my code: 这是我的代码:

public void MoveElement(UIElement uiElement)
{
    var element = ((FrameworkElement)uiElement).Parent;
    //TODO:Remove from parent
    myControl.Children.Add(uiElement);
}

When I hit the last statment an ArgumentException is thrown stating "Specified Visual is already a child of another Visual or the root of a CompositionTarget." 当我点击最后一个语句时,抛出ArgumentException,指出“指定的Visual已经是另一个Visual的子项或CompositionTarget的根”。 The strange thing is that Parent is returning null. 奇怪的是,Parent返回null。 How do locate the parent? 如何找到父母? Will this even work? 这甚至会起作用吗?

EDIT: I don't think actually moving an element is the answer to my problem. 编辑:我不认为实际移动元素是我的问题的答案。 I'm working with the Visual Studio SDK and was able to get a hold of the UIElement that makes up the editor pane (extends DockPanel ). 我正在使用Visual Studio SDK,并且能够获得构成编辑器窗格的UIElement (扩展DockPanel )。 I was trying to move the control from the standard editor into a custom tool window I'm developing. 我试图将控件从标准编​​辑器移动到我正在开发的自定义工具窗口中。

This is proving to be a hack and I realized that I need multiple instances of the same control so I think a more complex solution (and less of a hack) is in store. 这被证明是一个黑客,我意识到我需要同一控件的多个实例,所以我认为一个更复杂的解决方案(而不是一个黑客)存储。

The Parent property refers to the logical tree, and the docs note that "Parent may be null in cases where an element was instantiated, but is not attached to any logical tree that eventually connects to the page level root element, or the application object." Parent属性引用逻辑树,并且文档注意“在实例化元素但未连接到最终连接到页面级根元素或应用程序对象的任何逻辑树的情况下,Parent可以为null。 “ For example, the root element of a DataTemplate instantiated in a ListBox has a null Parent. 例如,在ListBox实例化的DataTemplate的根元素具有null Parent。

Try using VisualTreeHelper.GetParent instead. 请尝试使用VisualTreeHelper.GetParent The visual tree is the lower-level representation of how WPF elements are organised, and gives you access to all the extra "bits" that things like templating throw in there. 可视化树是WPF元素组织方式的低级表示,并允许您访问所有额外的“位”,其中包含模板化的内容。 For example, calling VisualTreeHelper.GetParent on the root element of a DataTemplate instantiated in a ListBox returns a ContentPresenter . 例如,在ListBox实例化的DataTemplate的根元素上调用VisualTreeHelper.GetParent将返回ContentPresenter

Note that just because you can get hold of the parent visual does not necessarily mean you'll be able to remove it. 请注意,仅仅因为您可以获得父视觉效果并不一定意味着您将能够删除它。 Some elements, such as Panels, provide methods for this. 某些元素(如Panel)为此提供了方法。 But if the element you locate is part of, say, a CheckBox , I don't think you'll be able to remove it. 但是如果你找到的元素是CheckBox ,我认为你不能删除它。

If you can provide a bit more context for what you're trying to achieve by moving controls around the visual tree, people may be able to offer more specific advice or alternative approaches. 如果您可以通过围绕可视化树移动控件来为您尝试实现的目标提供更多上下文,那么人们可能能够提供更具体的建议或替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM