mvvm viewmodel 关闭 view窗口
创建DialogCloser类
public class DialogCloser
{
public static readonly DependencyProperty DialogResultProperty =
DependencyProperty.RegisterAttached(
"DialogResult",
typeof(bool?),
typeof(DialogCloser),
new PropertyMetadata(DialogResultChanged));
private static void DialogResultChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var window = d as ChildWindow;
if (window != null)
window.DialogResult = e.NewValue as bool?;
}
public static bool? GetDialogResult(DependencyObject dp)
{
return (bool?)dp.GetValue(DialogResultProperty);
}
public static void SetDialogResult(DependencyObject target, bool? value)
{
target.SetValue(DialogResultProperty, value);
}
}
xaml
xmlns:local="clr-namespace:KTImager.BusinessApp.SLClient"
local:DialogCloser.DialogResult="{Binding DialogResult}">
viewmodel中
private bool? _dialogResult;
public bool? DialogResult
{
get { return _dialogResult; }
set
{
_dialogResult = value;
NotifyPropertyChanged(m => m.DialogResult);
}
}
public ICommand OKCommand
{
get
{
Action act = delegate()
{
DialogResult = true;
};
return new RelayCommand(act);
}
}