Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Does wpf user control contain a method to close, like close in windows form, thanks.
Updated 4-Jul-10 23:25pm

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();

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

I dont know what you wanted from this.
Share this answer
ihebiheb 28-Mar-13 13:37pm    
Window parentwin = Window.GetWindow(this);

this one worked for me
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.
Share this answer

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);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900