我如何提升属性更改属性更改

我如何提升属性更改属性更改

本文介绍了我如何提升属性更改属性更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我添加了一个模型给我的viewmodel ...

  public dal.UserAccount User {
get
{
return _user;
}
set
{
_user = value;
RaisePropertyChanged(String.Empty);
}
}

我处理属性更改事件...

 公共事件PropertyChangedEventHandler PropertyChanged; 
private void RaisePropertyChanged(string propertyName)
{
if(this.PropertyChanged!= null)
this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}

这是我使用的绑定。

 < TextBox Text ={Binding User.firstname,Mode = TwoWay,ValidatesOnDataErrors = True,UpdateSourceTrigger = PropertyChanged}/> 

问题是propertychange事件在更新视图时不触发?任何人都可以告诉我我做错了什么...

解决方案

PropertyChanged用于通知UI,该模型。
由于您正在更改用户对象的内部属性 - 用户属性本身不是因此更改了PropertyChanged事件。



第二 - 您的模型。 - 换句话说,确保 UserAccount 实现INotifyPropertyChanged,否则更改 firstname 不会影响视图。 >

另一件事:



RaisePropertyChanged应该接收的参数是属性的名称改变。所以在你的情况下:



更改:

RaisePropertyChanged(String.Empty); / p>



RaisePropertyChanged(User);



从 :

(无需刷新本例中的所有属性)



您可以阅读更多关于 PropertyChanged 的概念


Here I added a model to my viewmodel...

public dal.UserAccount User  {
  get
  {
    return _user;
  }
  set
  {
    _user = value;
    RaisePropertyChanged(String.Empty);
   }
}

I handle property change event...

public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
  if (this.PropertyChanged != null)
    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

This is the binding i use.

<TextBox Text="{Binding User.firstname, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />

Problem is propertychange event is not trigger on updating view ? Can anybody tell me what i am doing wrong...

解决方案

PropertyChanged is used to notify the UI that something has been changed in the Model.Since you're changing an inner property of the User object - the User property itself is not changed and therefore the PropertyChanged event isn't raised.

Second - your Model should implement the INotifyPropertyChanged interface. - In other words make sure UserAccount implements INotifyPropertyChanged, otherwise changing the firstname will not affect the view either.

Another thing:

The parameter RaisePropertyChanged should receive is the Name of the property that has changed. So in your case:

Change:
RaisePropertyChanged(String.Empty);

To
RaisePropertyChanged("User");

From MSDN:

(No need to refresh all the Properties in this case)

You can read more on the concept of PropertyChanged here

这篇关于我如何提升属性更改属性更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:46