PropertyChanged WPF MVVM Light
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34243936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PropertyChanged WPF MVVM Light
提问by Snovva1
I am using MVVM light in WPF. The Model class properties are constantly changed due to changes of the underlying data-access layer.
我在 WPF 中使用 MVVM 灯。由于底层数据访问层的变化,模型类属性不断变化。
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 Outputproperty 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?
如果Output属性更改,则会通知绑定,因此这是有效的。但是从知道新值的数据访问源更新属性的正确方法是什么?
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 RaisePropertyChangedcall in connectionhelper update the property of the model? If this is the wrong way, should I set up some kind of listener?
为什么RaisePropertyChangedconnectionhelper 中的调用不更新模型的属性?如果这是错误的方式,我应该设置某种侦听器吗?
回答by amuz
In your SBE_V1_Modelclass you should subscribe to receive PropertyChange notifications from the ConnectionHelperViewModel.
在您的SBE_V1_Model课程中,您应该订阅以接收来自ConnectionHelperViewModel 的PropertyChange 通知。
// 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 linkyou might be interested from StackOverflow.
还可以查看 MVVM 光信使。这是您可能对 StackOverflow 感兴趣的链接。
回答by Alvaro Royo
You should only use PropertyChanged in the view model, not at the Model. You can use PropertyChange in Model only in special times.
您应该只在视图模型中使用 PropertyChanged,而不能在模型中使用。您只能在特殊时间在模型中使用 PropertyChange。
RaisePropertyChanged("Output", false,b, true);
In that PropertyChanged you are alwais saying that Output Property was changed.
在那个 PropertyChanged 中,您总是说输出属性已更改。
I recommend you to implements INotifyPropertyChanged
我建议你实现 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");

