PropertyChanged WPF MVVM Light

只谈情不闲聊 提交于 2019-12-11 18:11:45

问题


I am using MVVM light in WPF. The Model class properties are constantly changed due to changes of the underlying data-access layer.

Model:

public class SBE_V1_Model : ViewModelBase
{
    public SBE_V1_Model(String name)
    {
        Name = "MAIN." + name;
        SetupClient();
    }
    private void SetupClient()
    {
        client =  new ConnectionHelper(this);
        client.Connect(Name);

    }
    public Boolean Output
    {
        get
        {
            return _Output;
        }
        set
        {
            if (value != this._Output)
            {
                Boolean oldValue = _Output;
                _Output = value;
                RaisePropertyChanged("Output", oldValue, value, true);
            }
        }
    }
}

If Output property changes, then bindings will be notified, so this works. But what is the correct way to update the property from the data-access source, which knows the new value?

public class ConnectionHelper : ViewModelBase
{
   public Boolean Connect(String name)
    {
        Name = name;
        tcClient = new TcAdsClient();

        try
        {
            dataStream = new AdsStream(4);
            binReader = new AdsBinaryReader(dataStream);
            tcClient.Connect(851);
            SetupADSNotifications();
            return true;
        }
        catch (Exception ee)
        {
            return false;
        }
    }
    private void tcClient_OnNotification(object sender, AdsNotificationEventArgs e)
    {
        String prop;
        notifications.TryGetValue(e.NotificationHandle, out prop);
        switch (prop)
        {
            case "Output":
                Boolean b = binReader.ReadBoolean();
                RaisePropertyChanged("Output", false,b, true);
                break;
     }
   }
 }

Why doesnt the RaisePropertyChanged call in connectionhelper update the property of the model? If this is the wrong way, should I set up some kind of listener?


回答1:


In your SBE_V1_Model class you should subscribe to receive PropertyChange notifications from the ConnectionHelper ViewModel.

// Attach EventHandler
ConnectionHelper.PropertyChanged += OnConnectionHelperPropertyChanged;

...

// When property gets changed, raise the PropertyChanged 
// event of the ViewModel copy of the property
OnConnectionHelperPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Something") //your ConnectionHelper property name
        RaisePropertyChanged("Ouput");
}

Also look into MVVM light messenger. Here is a link you might be interested from StackOverflow.




回答2:


You should only use PropertyChanged in the view model, not at the Model. You can use PropertyChange in Model only in special times.

RaisePropertyChanged("Output", false,b, true);

In that PropertyChanged you are alwais saying that Output Property was changed.

I recommend you to implements INotifyPropertyChanged

class MyClass : INotifyPropertyChanged
    {
      public bool MyProperty{ get; set; }

      public event PropertyChangedEventHandler PropertyChanged;

      protected void OnPropertyChanged(string name)
      {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
          handler(this, new PropertyChangedEventArgs(name));
         }
      }

    }

To notify any property change you have to use:

OnPropertyChanged("MyProperty");


来源:https://stackoverflow.com/questions/34243936/propertychanged-wpf-mvvm-light

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!