Search code examples
c#wpfmvvmstatusbar

C# WPF MVVM | Change Binded to StatusBar Vars


So, I have a statusbar as UserControl.

Model:

public class StatusBarModel : BindableBase
{
    private string _status;
    public string Status
    {
        get { return _status; }
        set
        {
            _status = value;
            RaisePropertyChanged("Status");
        }
    }

    private int _p_value;
    public int P_Value
    {
        get { return _p_value; }
        set
        {
            _p_value = value;
            RaisePropertyChanged("P_Value");
        }
    }

}

ViewModel:

 public class StatusBarVM : BindableBase
{
    readonly source.elements.StatusBar.StatusBarModel _model = new source.elements.StatusBar.StatusBarModel();
    public StatusBarVM()
    {
        _model.PropertyChanged += (s, e) => { RaisePropertyChanged(e.PropertyName); };

    }

    public string Status
    {
        get { return _model.Status; }
        set { _model.Status = value; }
    }

    public int P_Value
    {
        get { return _model.P_Value; }
        set { _model.P_Value = value; }
    }

}

And for example I wanna change Status variable from others ViewModels. How I can do it? I have seen examples with only buttons and etc.


Solution

  • There are multiple ways to achieve your requirement. as @bitclicker says, you can use static class that hold its value. But I think It is too much that makes it static class, because that variable value may be used only two viewmodel.

    I suggest you communicate between two view model. you will find Prism's event aggregator or you could implement your own event publish-subscriber model. making your own event pub-sub model would help you to make a first step into the design pattern.