0

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

 public class MyViewModel : BindableBase { }

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

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.

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

Clemens
  • 123,504
  • 12
  • 155
  • 268
IBRA
  • 1,502
  • 3
  • 24
  • 56

3 Answers3

0

You can use DependencyProperty in place of INotifyPropertyChanged.

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

Some Examples from the Interwebz

Community
  • 1
  • 1
  • Ok, I've never used DependencyProperty, It seems simple, but I couldn't get it to work. I think I'm missing something here. How can I set the new DP inside the ViewModel and then apply the changes into the UI? how to bind the new values to ComboBoxes – IBRA Mar 13 '17 at 10:46
  • @IBRA there are a lot of examples on SO for this http://stackoverflow.com/questions/15132538/twoway-bind-views-dependencyproperty-to-viewmodels-property – Smartis has left SO again Mar 13 '17 at 10:50
0

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

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:

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Inside the UpdateView method in the code above, the method gets the source parameter from a different project, which provides the same object for different platforms, WPF, web, etc... I was wondering if there is any way in order to notify the UI or SourceA property when the source object gets changed. – IBRA Mar 13 '17 at 14:01
  • No, you must raise the PropertyChanged event to notify the view whenever the source object gets changed and this requires you to be able to determine when the source object changes as I mentioned in the answer. The way to notify the UI *is* to raise the PropertyChanged event for all data bound source properties. – mm8 Mar 13 '17 at 14:25
  • Does that answer your question? – mm8 Mar 13 '17 at 17:06
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?

Community
  • 1
  • 1
ivica.moke
  • 1,054
  • 2
  • 10
  • 19