Move a UserControl from a ContentControl to another one programmatically

烈酒焚心 提交于 2019-12-03 07:53:41

Set

myContentControl1.Content = null;

to remove myUserControl from myContentControl1 before setting

myContentControl2.Content = myUserControl;

By the way, don't confuse the logical tree with the visual tree. Get more information in Trees in WPF in the MSDN.

In a ControlControl class description I can see a RemoveVisualChild method, but when I try to use it in code I get an Unknown method error

This is because RemoveVisualChild and RemoveLogicalChild are protected methods which you can not access in your class directly. If you want to use this method then create a derived class from ContentControl and expose these methods using some public method wrapper in that class.

Better option is to remove myUserControl from logical tree of myContentControl1 before adding it some other control's logical tree. To achieve this you can set the Content property of myContentControl1 to something else or to null.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!