Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close current UserControl

I have a Window1.xaml main Window; and after some event, I display a UserControl EditFile.xaml.

The code behind is:

public static int whichSelected = -1;
private void button1_Click(object sender, RoutedEventArgs e)
{
    //searchEditPanel.Children.Clear();
    whichSelected = listViewFiles.SelectedIndex;
    searchEditPanel.Children.Add(_EditFileControle);        //this is Grid
}

And now, how can I close the opened/added UserControl from its content by clicking a Cancel button or something like that?

like image 476
BlueMan Avatar asked Jun 01 '10 10:06

BlueMan


People also ask

How to close a UserControl in wpf?

Close from inside of UserControl, definitely you can do so using the following. container. Children. Remove(usercontrol);

How do I close a window in WPF?

Pressing ALT + F4 . Pressing the Close button. Pressing ESC when a button has the IsCancel property set to true on a modal window.


2 Answers

Window.GetWindow(this).Close();

You don't need to use a new variable, you can use it directly.

like image 176
Dino MARIANO Avatar answered Sep 30 '22 08:09

Dino MARIANO


You could set the Visibility property of the control you want to "close" to Collapsed.

This way it will not be displayed anymore but will still be present in the visual tree if you need to reuse it later.

like image 33
Pragmateek Avatar answered Sep 30 '22 06:09

Pragmateek