简体   繁体   English

从 WPF 中的 Window 删除控制

[英]Remove Control from Window in WPF

How can I remove a control from a window in WPF?如何从 WPF 中的 window 中删除控件? RemoveLogicalChild only removes it as a logical child, but leaves it still visible. RemoveLogicalChild仅将其作为逻辑子级删除,但仍然可见。

Every element in the visual tree is either the root of the tree, like a Window , or a child of another element.可视化树中的每个元素要么是树的根,如Window ,要么是另一个元素的子元素。 Ideally you would know which element is the parent of the element you are trying to remove and what type of FrameworkElement it is.理想情况下,您会知道哪个元素是您要删除的元素的父元素以及它是什么类型的FrameworkElement

For example, if you have a Canvas and many children and you have a Rectangle that was previously added to the Canvas , you can remove it from the visual tree by removing it from the Canvas like this:例如,如果您有一个Canvas和许多孩子,并且您有一个之前添加到CanvasRectangle ,您可以通过从Canvas中删除它来从可视树中删除它:

canvas.Children.Remove(control);

But if you don't know who the parent of the control is, you can use the VisualTreeHelper.GetParent Method to find out:但是如果你不知道控件的父级是谁,你可以使用VisualTreeHelper.GetParent 方法找出:

DependencyObject parent = VisualTreeHelper.GetParent(control);

The problem you now face is parent is a DependencyObject and while it is probably also a FrameworkElement , you don't know which kind of element it is.您现在面临的问题是parent是一个DependencyObject ,虽然它可能也是一个FrameworkElement ,但您不知道它是哪种元素。 This is important because how you remove the child depends on the type.这很重要,因为您如何移除孩子取决于类型。 If the parent is a Button , then you just clear the Content property.如果父级是Button ,那么您只需清除Content属性。 If the parent is a Canvas , you have to use Children.Remove .如果父级是Canvas ,则必须使用Children.Remove

In general, you can handle the most common cases by checking whether the item is a Panel and then remove from its children, otherwise if it is a ContentControl (like a Window ) then set its Content property to null .通常,您可以通过检查项目是否为Panel然后从其子项中删除来处理最常见的情况,否则如果它是ContentControl (如Window ),则将其Content属性设置为null But this isn't foolproof;但这并非万无一失。 there are other cases.还有其他情况。

You also have to be careful not to remove something that is expanded from a template because that is not a static content you can modify at will.您还必须注意不要删除从模板扩展的内容,因为这不是您可以随意修改的 static 内容。 If you added the control or existed in static XAML, you can safely remove it.如果您添加了控件或存在于 static XAML 中,则可以安全地删除它。

To check the parent type, You can also use the GetType Method adding toString Method and compare.要检查父类型,您还可以使用 GetType 方法添加 toString 方法并进行比较。 For example, the string "System.Windows.Controls.Canvas" will be returned when the parent Object is a canvas例如,当父 Object 是 canvas 时,将返回字符串“System.Windows.Controls.Canvas”

you can use this to remove a child from,in this case, a canvas.您可以使用它从 canvas 中移除一个孩子。

private void RemoveControl()
{
   name = myUserControl.GetValue(NameProperty).ToString();               
   myCanvas.Children.Remove(myUserControl);
   NameScope.GetNameScope(this).UnregisterName(name);
}

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

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