WPFが追加属性制御ウィンドウで閉じる
1300 ワード
シーン1
ウィンドウをShowDialog()方式で表示する場合、追加属性を定義することで、ViewModelでデータバインド(bool?)を実行できます.サブウィンドウの表示と閉じるを制御します.参照アドレス: Getting “DialogResult can be set only after Window is created and shown as dialog” when implementing WPF MVVM pattern for form closing DialogResult can be set only after Window is created and shown as dialog
シーン2
メインウィンドウの表示とクローズもViewModelで制御したい場合はイベントとメッセージレベル方式で実現できますが、具体的にはMVVM LightのMessengerの使い方を参考にしてください
転載先:https://www.cnblogs.com/hippieZhou/p/9898258.html
ウィンドウをShowDialog()方式で表示する場合、追加属性を定義することで、ViewModelでデータバインド(bool?)を実行できます.サブウィンドウの表示と閉じるを制御します.
public class ExWindow
{
public static bool? GetDialogResult(DependencyObject obj)
{
return (bool?)obj.GetValue(DialogResultProperty);
}
public static void SetDialogResult(DependencyObject obj, bool value)
{
obj.SetValue(DialogResultProperty, value);
}
// Using a DependencyProperty as the backing store for DialogResult. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DialogResultProperty =
DependencyProperty.RegisterAttached("DialogResult", typeof(bool?), typeof(ExWindow), new PropertyMetadata(true, (d, e) =>
{
var handler = d as Window;
handler.DialogResult = e.NewValue as bool?;
}));
}
シーン2
メインウィンドウの表示とクローズもViewModelで制御したい場合はイベントとメッセージレベル方式で実現できますが、具体的にはMVVM LightのMessengerの使い方を参考にしてください
転載先:https://www.cnblogs.com/hippieZhou/p/9898258.html