【问题标题】:Closing a Window from its content's ViewModel in MVVM WPF?在 MVVM WPF 中从其内容的 ViewModel 中关闭一个窗口?
【发布时间】:2014-03-07 17:16:40
【问题描述】:

像这样创建一个窗口,使用我自定义的UserControl 作为内容:

Window newCacheForm = new Window
{
    Title = "Add New Cache Tag",
    Content = new NewCacheControl()
};

我想以对话框的形式打开Window 并获得结果:

var result = newCacheForm.ShowDialog();

我有代码可以绑定并将对话框设置为 true 或 false,但是如何从 UserControl ViewModel 关闭窗口?如果无法做到这一点,我该如何以对 MVVM 友好的方式进行处理?

【问题讨论】:

    标签: c# wpf mvvm user-controls window


    【解决方案1】:

    在这种情况下,我将使用 attached 行为,它允许在View 一侧使用独立逻辑。我个人没有创建它,而是使用了 here 并进行了少量补充 - 将 Get() 添加到依赖属性中。

    以下是此行为的完整代码:

    public static class WindowCloseBehaviour
    {
        public static bool GetClose(DependencyObject target)
        {
            return (bool)target.GetValue(CloseProperty);
        }
    
        public static void SetClose(DependencyObject target, bool value)
        {
            target.SetValue(CloseProperty, value);
        }
    
        public static readonly DependencyProperty CloseProperty = DependencyProperty.RegisterAttached("Close",
                                                                                                      typeof(bool),
                                                                                                      typeof(WindowCloseBehaviour),
                                                                                                      new UIPropertyMetadata(false, OnClose));
    
        private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is bool && ((bool)e.NewValue))
            {
                Window window = GetWindow(sender);
    
                if (window != null)
                    window.Close();
            }
        }
    
        private static Window GetWindow(DependencyObject sender)
        {
            Window window = null;
    
            if (sender is Window)
                window = (Window)sender;
    
            if (window == null)
                window = Window.GetWindow(sender);
    
            return window;
        }
    }
    

    在创建新 Window 的 Click 处理程序中,我添加了这样的行为:

    private void Create_Click(object sender, RoutedEventArgs e)
    {
        Window newCacheForm = new Window
        {
            Title = "Add New Cache Tag",
            Content = new TestUserControl(),
            DataContext = new TestModel() // set the DataContext
        };
    
        var myBinding = new Binding();                // create a Binding
        myBinding.Path = new PropertyPath("IsClose"); // with property IsClose from DataContext
        newCacheForm.SetBinding(WindowCloseBehaviour.CloseProperty, myBinding); // for attached behavior 
    
        var result = newCacheForm.ShowDialog();
    
        if (result == false) 
        {
            MessageBox.Show("Close Window!");
        }
    }
    

    UserControl 的关闭处理程序中写下:

    private void Close_Click(object sender, RoutedEventArgs e)
    {
        TestModel testModel = this.DataContext as TestModel;
        testModel.IsClose = true;
    }
    

    当然,应该使用命令而不是按钮的Click 处理程序。

    整个项目可用here

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2011-03-09
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      相关资源
      最近更新 更多