且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在属性更改时 RaisePropertyChanged?

更新时间:2023-02-13 19:16:40

PropertyChanged 用于通知 UI 模型中的某些内容已更改.由于您要更改 User 对象的 inner 属性 - User 属性本身不会更改,因此不会引发 PropertyChanged 事件.

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.

第二 - 你的模型 应该实现 INotifyPropertyChanged 接口.- 换句话说,确保 UserAccount 实现 INotifyPropertyChanged,否则更改 firstname 也不会影响视图.

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.

另一件事:

RaisePropertyChanged 应接收的参数是已更改属性的名称.所以在你的情况下:

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

变化:
RaisePropertyChanged(String.Empty);


RaisePropertyChanged("User");

来自 MSDN:

PropertyChanged 事件可以通过使用 null 或 String.Empty 作为 PropertyChangedEventArgs 中的属性名称来指示对象上的所有属性都已更改.

The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.

(在这种情况下无需刷新所有属性)

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

您可以阅读有关 PropertyChanged 概念的更多信息 这里