Overcorrect, going from one extreme to the other. MVVM mode, View is only responsible for the presentation, although there is background code, but basically is the decoration, VM takes over all the logical processing.

So, now that the power VM has done everything, how can this window V close automatically?

From what little I know of WPF so far, there are two options:

Use the IsEnable property in the View.

It works like this:

1. IsEnabled in the UI binds the VM attributes

2. In the background code of UI, register IsEnableChange event. In this event, the Close() command can be triggered if the value passed meets a certain condition

In this way, the VM can control its own property to achieve the purpose of turning V off.

No code, I’ll say J8:

UI

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"...IsEnabled="{Binding IsLoginFailed, Mode=OneWay}">
Copy the code

UI background code

        public LoginWindow()
        {
            InitializeComponent();

            this.IsEnabledChanged += LoginWindow_IsEnabledChanged;
        }
        private void LoginWindow_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == false)
            {
                this.Close(); }}Copy the code

VM

        private bool isLoginFailed = true;
        /// <summary>
        /// <para>Gets or sets a value indicating whether a login failed; True indicates that the login fails; otherwise, false.</para>
        /// <para>Bind to the IsEnable property of LoginWindow. When False, close the Login Window.</para>
        ///Notify the client when this property changes.
        /// </summary>
        public bool IsLoginFailed
        {
            get
            {
                return this.isLoginFailed;
            }

            set
            {
                if (this.isLoginFailed ! =value)
                {
                    this.isLoginFailed = value;
                    this.RaisePropertyChanged("IsLoginFailed"); }}}Copy the code


\

Second, using the Invoke

1. UI background code:

        public LoginWindow()
        {
            InitializeComponent();

            this.DataContext = new LoginWindowVM(this.Close);
        }
Copy the code

2. VM code:

        Action _closeAction;
        public LoginWindowVM(Action closeAction)
        {
            InitializeLoginInfo();
            this._closeAction = closeAction;
        }
        void CloseWin()
        {
            App.Current.Dispatcher.Invoke((Action)(() =>
            {
                this._closeAction.Invoke();
            }));
        }
Copy the code

There are two methods, no doubt the first one is more in line with MVVM ideas. VM sends a signal to close, triggering V to close. How to close and when to close are controlled by V itself.

And plan two, through the way to call directly closed, like a bit of overreach?

However, IsEnable, which is intended to control invalidation and enablement, is used to turn off the UI, instead of controlling invalidation and enablement, which is sometimes inconvenient.

Plan 2 reference article:

Bbs.csdn.net/topics/3600…


\