views:

30

answers:

2

I have a Window1.xaml main Window and after same 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 to close the opened/added UserControl from it content by clicking Cancel button or something like that.

A: 

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.

Serious
A: 

Have you tried this?

searchEditPanel.Children.Remove(_EditFileControle);

Another Suggestion:

Maybe this helps: http://sachabarber.net/?p=162

if it doesn't: Add a property to your UserControl:

public UserControl ParentControl {get;set;}

Now modify your code:

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

Now you should be able to do this:

 // Somewhere in your UserControl
if (this.ParentControl != null)
    this.ParentControl.Children.Remove(this);
SchlaWiener
Yes - that works, but running from parent (Window1). I must close UserControl from my children (EditFile.xaml.cs file) by clicking on it some Cancel button.
BlueMan