关闭当前用户控件控件、用户

2023-09-07 15:46:02 作者:〆精心制造美丽旳风景線◆

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

后面的代码是:

public static int whichSelected = -1;私人无效按钮1_Click(对象发送者,RoutedEventArgs e){//searchEditPanel.Children.Clear();whichSelected = listViewFiles.SelectedIndex;searchEditPanel.Children.Add(_EditFileControle);//这是网格}
C 解决VS目标平台设为X64,用户控件不可用,显示控件未能加载,将从工具箱把它移除

现在,如何通过单击取消"按钮或类似的东西从其内容中关闭打开/添加的 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);