Best way to enable/disable controls on different V

2019-08-04 04:37发布

I have a application that I am developing. It can be in two states (Connected and Disconnected). There is a boolean property in my MainViewModel that keeps track of the current state.

I have many other Views (and ViewModels) in my application. When the application goes into a Disconnected state I need to make several controls (not all) in each view disabled. When the applicaiton is in the connected state then obviously I need to enable those same controls.

I am wondering what would be a good way to accomplish this?

标签: c# wpf mvvm
3条回答
SAY GOODBYE
2楼-- · 2019-08-04 05:03

Viewmodel locator pattern is one option, and not a bad one.

I would recommend installing mvvm light from nuget as that plugs in a template for you.

Another alternative is to use the messaging framework of mvvm light. In this scenario you're main view model issues a message when your state changes which your other view models, assumed to be bound to your views, can subscribe to.

This has the advantage that each view model can respond to that behaviour in whatever way. For those just requiring the bool for binding, create a base view model which registers to the message and updates a property. For other view models you might want to alter can execute command bindings or update labels etc.

查看更多
Juvenile、少年°
3楼-- · 2019-08-04 05:15

I guess you have only one instance of your MainViewModel.

So expose this unique instance through a static property, and even make it a singleton.

This way you can easily share your connection status between views.

using System.ComponentModel;

namespace WpfMagic
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private static readonly MainViewModel instance = new MainViewModel();

        public static MainViewModel Instance { get { return instance; } }

        private bool isConnected;
        public bool IsConnected
        {
            get { return isConnected; }
            set
            {
                if (value != isConnected)
                {
                    isConnected = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("IsConnected"));
                }
            }
        }

        private MainViewModel()
        {
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}

The tricky part is the static binding, but otherwise it's simple:

Your first view:

<Button IsEnabled="{Binding Path=(local:MainViewModel.Instance).IsConnected}">Send Spams</Button>

Another one:

<Button IsEnabled="{Binding Path=(local:MainViewModel.Instance).IsConnected}">DDOS SO</Button>

And a last one:

<Button IsEnabled="{Binding Path=(local:MainViewModel.Instance).IsConnected}">Open Lol Cats Videos</Button>

To test it you can use a CheckBox in yet another view:

<CheckBox IsChecked="{Binding Path=(local:MainViewModel.Instance).IsConnected}">Is Connected?</CheckBox>
查看更多
Anthone
4楼-- · 2019-08-04 05:17

You could use ViewModelLocator and have a singleton MainViewModel within there. This will allow you to access the MainVM from any ViewModel. From there directly access the bool property needed.

Hope this helps. ViewModelLocator IOC Containers, MVVM ViewModelLocator

查看更多
登录 后发表回答