Close current UserControl

孤街醉人 提交于 2019-12-03 16:49:49

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);
Dino MARIANO
Window.GetWindow(this).Close();

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

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.

In your button click handler try :

Window parentWindow = (Window)this.Parent;
parentWindow.Close();
AliAzra
private void Button_Click(object sender, RoutedEventArgs e)
{

    (this.Parent as searchEditPanel).Children.Remove(this);

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