WPF MVVM: How to close a window

后端 未结 21 1815
后悔当初
后悔当初 2020-12-04 08:19

I have a Button that closes my window when it\'s clicked:


21条回答
  •  旧时难觅i
    2020-12-04 09:00

    I also had to deal with this problem, so here my solution. It works great for me.

    1. Create class DelegateCommand

        public class DelegateCommand : ICommand
    {
        private Predicate _canExecuteMethod;
        private readonly Action _executeMethod;
        public event EventHandler CanExecuteChanged;
    
        public DelegateCommand(Action executeMethod) : this(executeMethod, null)
        {
        }
        public DelegateCommand(Action executeMethod, Predicate canExecuteMethod)
        {
            this._canExecuteMethod = canExecuteMethod;
            this._executeMethod = executeMethod ?? throw new ArgumentNullException(nameof(executeMethod), "Command is not specified."); 
        }
    
    
        public void RaiseCanExecuteChanged()
        {
            if (this.CanExecuteChanged != null)
                CanExecuteChanged(this, null);
        }
        public bool CanExecute(object parameter)
        {
            return _canExecuteMethod == null || _canExecuteMethod((T)parameter) == true;
        }
    
        public void Execute(object parameter)
        {
            _executeMethod((T)parameter);
        }
    }
    

    2. Define your command

            public DelegateCommand CloseWindowCommand { get; private set; }
    
    
        public MyViewModel()//ctor of your viewmodel
        {
            //do something
    
            CloseWindowCommand = new DelegateCommand(CloseWindow);
    
    
        }
            public void CloseWindow(Window win) // this method is also in your viewmodel
        {
            //do something
            win?.Close();
        }
    

    3. Bind your command in the view

    public MyView(Window win) //ctor of your view, window as parameter
        {
            InitializeComponent();
            MyButton.CommandParameter = win;
            MyButton.Command = ((MyViewModel)this.DataContext).CloseWindowCommand;
        }
    

    4. And now the window

      Window win = new Window()
            {
                Title = "My Window",
                Height = 800,
                Width = 800,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
    
            };
            win.Content = new MyView(win);
            win.ShowDialog();
    

    so thats it, you can also bind the command in the xaml file and find the window with FindAncestor and bind it to the command parameter.

提交回复
热议问题