如何确保我的子窗口在关闭时被卸载?
我正在从我的视图模型中打开子窗口,但是在它关闭后,它仍然会触发诸如组合框上的 selectionchanged 之类的事件。
子窗口使用与调用它的视图模型相同的视图模型,所以我想这解释了为什么事件被触发。itemssources 仍然有效。
但是当它关闭时,我想永远“处置”子窗口。
我试图像这样添加一个封闭的处理程序(默认视图代码后面):
private void OnLaunchEditItem(ItemMessage msg)
{
var editWnd = new EditItemWindow();
editWnd.Closed += new EventHandler(editWnd_Closed);
editWnd.Show();
}
void editWnd_Closed(object sender, EventArgs e)
{
sender = null;
}
没有成功。。
所以我现在正在做的是从子窗口控件中删除 itemssource,在我看来这不是解决问题的理想方法。必须有可能在关闭时将其全部从内存中处理掉?(子窗口“查看”代码隐藏)
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
combobox1.ItemsSource = null;
combobox2.ItemsSource = null;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
combobox1.ItemsSource = null;
combobox2.ItemsSource = null;
}