关闭当前用户控件 [英] Close current UserControl

查看:25
本文介绍了关闭当前用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Window1.xaml 主窗口;在某些事件之后,我会显示一个 UserControl EditFile.xaml.

后面的代码是:

public static int whichSelected = -1;私人无效按钮1_Click(对象发送者,RoutedEventArgs e){//searchEditPanel.Children.Clear();whichSelected = listViewFiles.SelectedIndex;searchEditPanel.Children.Add(_EditFileControle);//这是网格}

现在,如何通过单击取消"按钮或类似的东西从其内容中关闭打开/添加的 UserControl?

解决方案

你试过了吗?

searchEditPanel.Children.Remove(_EditFileControle);

另一个建议:

也许这会有所帮助:http://sachabarber.net/?p=162p>

如果没有:将属性添加到您的 UserControl:

public UserControl ParentControl {get;set;}

现在修改你的代码:

private void button1_Click(object sender, RoutedEventArgs e){//searchEditPanel.Children.Clear();whichSelected = listViewFiles.SelectedIndex;_EditFileControle.ParentControl = 这个;searchEditPanel.Children.Add(_EditFileControle);//这是网格}

现在你应该可以这样做了:

//在你的 UserControl 中的某个地方如果(this.ParentControl!= null)this.ParentControl.Children.Remove(this);

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?

解决方案

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

这篇关于关闭当前用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆