How to cancel window closing in MVVM WPF application

前端 未结 3 901
滥情空心
滥情空心 2021-02-13 11:22

How can I cancel exiting from particular form after Cancel button (or X at the top right corner, or Esc) was clicked?

WPF:



        
相关标签:
3条回答
  • 2021-02-13 12:05

    You are trying to do View's work in ViewModel class. Let your View class to handle the closing request and whether it should be canceled or not.

    To cancel closing of a window you can subscribe to the Closing event of view and set CancelEventArgs.Cancel to true after showing a MessageBox.

    Here is an example:

    <Window
        ...
        x:Class="MyApp.MyView"
        Closing="OnClosing"
        ...
    />
    </Window>
    

    Code behind:

    private void OnClosing(object sender, CancelEventArgs e)
    {
        var result = MessageBox.Show("Really close?",  "Warning", MessageBoxButton.YesNo);
        if (result != MessageBoxResult.Yes)
        {
            e.Cancel = true;
        }
    
        // OR, if triggering dialog via view-model:
    
        bool shouldClose = ((MyViewModel) DataContext).TryClose();
        if(!shouldClose)
        {
            e.Cancel = true;
        }
    }
    
    0 讨论(0)
  • 2021-02-13 12:06

    I'm not an MVVM expert, but in my opinion Yusufs' answer isn't quite MVVM. On the other hand Torpederos answer is a bit complicated for only close cancellation. Here is my approach. In this example I subscribed to the closing event, but it is always cancelled

    private void OnClosing(object sender, CancelEventArgs e)
    {
        e.Cancel = true;
        return;
    }
    

    In the XAML I added this

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <i:InvokeCommandAction Command="{Binding Close}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    

    And finally in the view model

    public ICommand Close { get; set; }
    Close = new RelayCommand(CommandClose);
    private void CommandClose(object sender)
    {
        if (Dirty)
        {
            // Save your data here
        }
        Environment.Exit(0);
    }
    

    In this approach the the closing event is triggered first. That cancels the closing. After that the interaction trigger is invoked and triggers the code in the view model via the RelayCommand. In the view model I can use the Dirty flag that is not accessible in the view.

    0 讨论(0)
  • 2021-02-13 12:06

    Very good example of doing this in the View Model way can be found in the article of Nish Nishant, where he's using attached properties to hook up window events with commands.

    Sample code of attached behaviour (author of the code: Nish Nishant)

    public class WindowClosingBehavior {
    
        public static ICommand GetClosed(DependencyObject obj) {
            return (ICommand)obj.GetValue(ClosedProperty);
        }
    
        public static void SetClosed(DependencyObject obj, ICommand value) {
            obj.SetValue(ClosedProperty, value);
        }
    
        public static readonly DependencyProperty ClosedProperty 
            = DependencyProperty.RegisterAttached(
            "Closed", typeof(ICommand), typeof(WindowClosingBehavior),
            new UIPropertyMetadata(new PropertyChangedCallback(ClosedChanged)));
    
        private static void ClosedChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
    
            Window window = target as Window;
    
            if (window != null) {
    
                if (e.NewValue != null) {
                    window.Closed += Window_Closed;
                }
                else {
                    window.Closed -= Window_Closed;
                }
            }
        }
    
        public static ICommand GetClosing(DependencyObject obj) {
            return (ICommand)obj.GetValue(ClosingProperty);
        }
    
        public static void SetClosing(DependencyObject obj, ICommand value) {
            obj.SetValue(ClosingProperty, value);
        }
    
        public static readonly DependencyProperty ClosingProperty 
            = DependencyProperty.RegisterAttached(
            "Closing", typeof(ICommand), typeof(WindowClosingBehavior),
            new UIPropertyMetadata(new PropertyChangedCallback(ClosingChanged)));
    
        private static void ClosingChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
    
            Window window = target as Window;
    
            if (window != null) {
    
                if (e.NewValue != null) {
                    window.Closing += Window_Closing;
                }
                else {
                    window.Closing -= Window_Closing;
                }
            }
        }
    
        public static ICommand GetCancelClosing(DependencyObject obj) {
            return (ICommand)obj.GetValue(CancelClosingProperty);
        }
    
        public static void SetCancelClosing(DependencyObject obj, ICommand value) {
            obj.SetValue(CancelClosingProperty, value);
        }
    
        public static readonly DependencyProperty CancelClosingProperty 
            = DependencyProperty.RegisterAttached(
            "CancelClosing", typeof(ICommand), typeof(WindowClosingBehavior));
    
        static void Window_Closed(object sender, EventArgs e) {
    
            ICommand closed = GetClosed(sender as Window);
    
            if (closed != null) {
                closed.Execute(null);
            }
        }
    
        static void Window_Closing(object sender, CancelEventArgs e) {
    
            ICommand closing = GetClosing(sender as Window);
    
            if (closing != null) {
    
                if (closing.CanExecute(null)) {
                    closing.Execute(null);
                }
                else {
    
                    ICommand cancelClosing = GetCancelClosing(sender as Window);
    
                    if (cancelClosing != null) {
                        cancelClosing.Execute(null);
                    }
    
                    e.Cancel = true;
                }
            }
        }
    }   
    

    Example how to bind commands:

    <Window 
        x:Class="WindowClosingDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:nsmvvm="clr-namespace:NS.MVVM"
        nsmvvm:WindowClosingBehavior.Closed="{Binding ClosedCommand}"
        nsmvvm:WindowClosingBehavior.Closing="{Binding ClosingCommand}"
        nsmvvm:WindowClosingBehavior.CancelClosing="{Binding CancelClosingCommand}">
    

    Commands "ClosedCommand", "ClosingCommand" and "CancelClosingCommand" should be defined in the separate View-Model.

    internal class MainViewModel : ViewModelBase {
    
        private ObservableCollection<string> log = new ObservableCollection<string>();
    
        public ObservableCollection<string> Log {
            get { return log; }
        }
    
        private DelegateCommand exitCommand;
    
        public ICommand ExitCommand {
    
            get {
    
                if (exitCommand == null) {
                    exitCommand = new DelegateCommand(Exit);
                }
    
                return exitCommand;
            }
        }
    
        private void Exit() {
            Application.Current.Shutdown();
        }
    
        private DelegateCommand closedCommand;
    
        public ICommand ClosedCommand {
    
            get {
    
                if (closedCommand == null) {
                    closedCommand = new DelegateCommand(Closed);
                }
    
                return closedCommand;
            }
        }
    
        private void Closed() {
            log.Add("You won't see this of course! Closed command executed");
            MessageBox.Show("Closed");
        }
    
        private DelegateCommand closingCommand;
    
        public ICommand ClosingCommand {
    
            get {
    
                if (closingCommand == null) {
                    closingCommand = new DelegateCommand(ExecuteClosing, CanExecuteClosing);
                }
    
                return closingCommand;
            }
        }
    
        private void ExecuteClosing() {
            log.Add("Closing command executed");
            MessageBox.Show("Closing");
        }
    
        private bool CanExecuteClosing() {
    
            log.Add("Closing command execution check");
    
            return MessageBox.Show("OK to close?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes;
        }
    
        private DelegateCommand cancelClosingCommand;
    
        public ICommand CancelClosingCommand {
    
            get {
    
                if (cancelClosingCommand == null) {
                    cancelClosingCommand = new DelegateCommand(CancelClosing);
                }
    
                return cancelClosingCommand;
            }
        }
    
        private void CancelClosing() {
            log.Add("CancelClosing command executed");
            MessageBox.Show("CancelClosing");
        }
    }
    
    0 讨论(0)
提交回复
热议问题