且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在WPF表单应用程序中关闭WPF用户控件

更新时间:2023-01-26 12:16:52

好吧UserControl和Form是两件不同的事情. UserControls放置在窗口内.您有一个与Window绑定的Close方法.如果要从UserControl内部强制执行Window.Close,则绝对可以使用以下操作.

Well UserControl and Form are two different things. UserControls are placed inside the Window. You have a Close method tied up with Window. If you want to force the Window.Close from inside of UserControl, definitely you can do so using the following.

Window parentwin = Window.GetWindow();
parentwin.Close();



但是,如果您只想关闭UserControl,为什么不处理Visibility.您可以使UserControl.Visibility = Collapsed,也可以使用
从父级删除UserControl. container.Children.Remove(usercontrol);

我不知道您想要什么.



But if you want to close only the UserControl, why dont you deal with Visibility. You can make the UserControl.Visibility = Collapsed, or you can remove the UserControl from the Parent using
container.Children.Remove(usercontrol);

I dont know what you wanted from this.


这是一个疯狂的问题.控件在窗体上时如何关闭"?这是零意义.如果要关闭"控件,则可以对其进行动画处理,或仅将其可见性设置为折叠(或同时显示).控件无法关闭",因为它不是一种形式,在我从未使用过的任何框架中都无法使用.
This is an insane question. How would a control ''close'' when it is on a form ? That makes zero sense. If you want to ''close'' a control, you can animate it, or just set it''s visibility to collapsed ( or both ). A control can''t be ''closed'' because it is not a form, not in any framework I''ve ever used.


Ummm

您可以从父控件中删除该控件.这是我假设父控件是Canvas所做的事情
Ummm

You could just remove the control from the parent Control. This is what I did assuming that the parent control is a Canvas
(this.Parent as Canvas).Children.Remove(this);