如何在没有实现INotifyPropertyChanged的对象时更新绑定。 WPF

时间:2022-09-25 18:39:43

I have a ViewModel which inherits from BindableBase class, it is a class from Prism that implements INotifyPropertyChanged.

我有一个继承自BindableBase类的ViewModel,它是一个来自Prism的实现INotifyPropertyChanged的类。

 public class MyViewModel : BindableBase { }

I have a Property that uses SetProperty method to notify the UI with any changes.

我有一个使用SetProperty方法的属性通知UI任何更改。

private ClassA _sourceA;
public ClassA  SourceA {
    get { return _sourceA; }
    set { SetProperty(ref _sourceA, value); }
}

The property is a reference from another source object from code behind.

该属性是来自代码后面的另一个源对象的引用。

private void UpdateView(ClassA source) {
    SourceA = source;// the source object does not implement INotifyPropertyChanged
}

When user changes one value from the UI XAML, some other values will be changed accordingly in the source object from code behind.

当用户从UI XAML更改一个值时,其他一些值将在源代码对象中相应地从代码后面更改。

How to update the UI when the source (from UpdateView method) object gets updated?

如何在源(来自UpdateView方法)对象更新时更新UI?

3 个解决方案

#1


0  

You can use DependencyProperty in place of INotifyPropertyChanged.

您可以使用DependencyProperty代替INotifyPropertyChanged。

DependencyProperty.Register("SourceA", typeof(ClassA), typeof(MainWindow));

Some Examples from the Interwebz

#2


0  

How to update the UI when the source (from UpdateView method) object gets updated?

如何在源(来自UpdateView方法)对象更新时更新UI?

You need to raise the PropertyChanged event of the view model for each source property that is bound to a target property in the view, i.e. for each property of the source object that you want to updated in the view you should call the OnPropertyChanged method of the BindableBase class:

您需要为视图中绑定到目标属性的每个源属性引发视图模型的PropertyChanged事件,即对于要在视图中更新的源对象的每个属性,您应该调用该属性的OnPropertyChanged方法。 BindableBase类:

OnPropertyChanged("Property1");
OnPropertyChanged("Property2");

...

You need to do this whenever a property of the source object is changed. This means that the source object must raise an event or something to notify any other classes of this.

只要更改源对象的属性,就需要执行此操作。这意味着源对象必须引发事件或某事以通知任何其他类。

#3


0  

If that model is somewhere else and you can't change it (cant implement INPC), maybe you should make some wrapper model that mimics that model, and on that wrapper model implement INPC. That way your UI will be updated. Maybe this link will help- What is a wrapper class?

如果该模型在其他地方并且您无法更改它(无法实现INPC),那么您可能应该制作一些模仿该模型的包装器模型,并在该包装器模型上实现INPC。这样你的UI就会更新。也许这个链接会有所帮助 - 什么是包装类?

#1


0  

You can use DependencyProperty in place of INotifyPropertyChanged.

您可以使用DependencyProperty代替INotifyPropertyChanged。

DependencyProperty.Register("SourceA", typeof(ClassA), typeof(MainWindow));

Some Examples from the Interwebz

#2


0  

How to update the UI when the source (from UpdateView method) object gets updated?

如何在源(来自UpdateView方法)对象更新时更新UI?

You need to raise the PropertyChanged event of the view model for each source property that is bound to a target property in the view, i.e. for each property of the source object that you want to updated in the view you should call the OnPropertyChanged method of the BindableBase class:

您需要为视图中绑定到目标属性的每个源属性引发视图模型的PropertyChanged事件,即对于要在视图中更新的源对象的每个属性,您应该调用该属性的OnPropertyChanged方法。 BindableBase类:

OnPropertyChanged("Property1");
OnPropertyChanged("Property2");

...

You need to do this whenever a property of the source object is changed. This means that the source object must raise an event or something to notify any other classes of this.

只要更改源对象的属性,就需要执行此操作。这意味着源对象必须引发事件或某事以通知任何其他类。

#3


0  

If that model is somewhere else and you can't change it (cant implement INPC), maybe you should make some wrapper model that mimics that model, and on that wrapper model implement INPC. That way your UI will be updated. Maybe this link will help- What is a wrapper class?

如果该模型在其他地方并且您无法更改它(无法实现INPC),那么您可能应该制作一些模仿该模型的包装器模型,并在该包装器模型上实现INPC。这样你的UI就会更新。也许这个链接会有所帮助 - 什么是包装类?