属性已更改的WPF MVVM灯 [英] PropertyChanged WPF MVVM Light

查看:87
本文介绍了属性已更改的WPF MVVM灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中使用MVVM灯.由于基础数据访问层的更改,因此Model类的属性不断变化.

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

型号:

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);
            }
        }
    }
}

如果Output属性更改,则将通知绑定,因此可行.但是,从知道新值的数据访问源更新属性的正确方法是什么?

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;
     }
   }
 }

为什么connectionhelper中的RaisePropertyChanged调用不更新模型的属性?如果这是错误的方法,我应该设置某种侦听器吗?

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?

推荐答案

在您的SBE_V1_Model类中,您应该订阅以接收来自ConnectionHelper ViewModel的PropertyChange通知.

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");
}

还可以查看MVVM灯光信使.这是您可能对StackOverflow感兴趣的链接.

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

这篇关于属性已更改的WPF MVVM灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆