从父viewmodel wpf mvvm关闭儿童窗口

时间:2013-07-15 12:14:39

标签: c# wpf mvvm window

点击确定/取消后关闭子Window

我从父mvm模型调用子窗口:

//parent view model
var optionSetViewModel = new OptionSetViewModel();
var optionSet = new OptionSet();
optionSet.SetViewModel(optionSetViewModel);

optionSet.SetOwner(_componentview);
optionSet.ShowDialog();

Window的ok和cancel代码写在子视图模型中:

private void OkClick()
{           
    _childWindow.Close();
}

它关闭很好但是当它从它被调用的父视图模型返回时关闭它会抛出空引用异常。关闭后,我正在访问子窗口值:

_someText = optionSetViewModel.SomeText;
_noteText = optionSetViewModel.NoteText;
_optionsetLanguage = optionSetViewModel.OptionSetSelectedItem;
_optionsetselected = optionSetViewModel.OptionSetSelected.ToString();

1 个答案:

答案 0 :(得分:1)

使用Catel(请参阅http://www.catelproject.com/),您可以根据视图模型关闭视图。因此,关闭视图意味着这样的调用:

myChildViewModel.CloseViewModel();

显示子窗口:

var myChildViewModel = new MyChildViewModel();
uiVisualizerService.Show(myChildViewModel);