是否可以更改 WPF 控件的父级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17222161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:02:59  来源:igfitidea点击:

Is it possible to change parent of WPF control

c#wpfwpf-controls

提问by Dilshod

Is it possible to change the parent of a WPF control? Here is an example:

是否可以更改 WPF 控件的父级?下面是一个例子:

StackPanelstack1 has Buttonbtn1 in it. There is another empty StackPanelstack2. I want to move the btn1 to stack2 programmatically.

StackPanelstack1 中有Buttonbtn1。还有另一个空的StackPanelstack2。我想以编程方式将 btn1 移动到 stack2。

Thanks for the help.

谢谢您的帮助。

回答by Reed Copsey

You can do this via the StackPanel.Childrenproperty:

您可以通过该StackPanel.Children属性执行此操作:

stack1.Children.Remove(btn1);
stack2.Children.Add(btn1);