Implementing “close window” command with MVVM

后端 未结 12 1703
既然无缘
既然无缘 2020-12-05 09:31

So my first attempt did everything out of the code behind, and now I\'m trying to refactor my code to use the MVVM pattern, following the guidance of the MVVM in the box inf

12条回答
  •  臣服心动
    2020-12-05 10:09

    Here is the simplest and pure MVVM solution

    ViewModel Code

    public class ViewModel
    {
        public Action CloseAction { get; set; }
    
        private void CloseCommandFunction()
        {
            CloseAction();
        }
    }
    

    Here is XAML View Code

    public partial class DialogWindow : Window
    {
        public DialogWindow()
        {
            ViewModel vm = new ViewModel();
            this.DataContext = vm;
    
            vm.CloseAction = new Action(() => this.Close());
        }
    }
    

提交回复
热议问题