How to react to model changes? - wpf

I would like to know what the "best practice" is for the following scenario:-
I have a viewmodel that exposes a model via a public property. The model is a C# class (separate from the viewmodel) that implements INotifyPropertyChanged. In my view I bind textboxes to various properties of that model.
I would like to execute a method in my viewmodel whenever the model's property values change. What would be a good approach? My thoughts:-
Have the viewmodel pass a delegate to the model which gets called by the property's setter, or
Have the viewmodel subscribe to the model's PropertyChanged event?
The first feels like I would be contaminating what is essentially a POCO model, while the second option somehow doesn't feel quite right either - subscribing to an event that is normally used by views/binding.

Have the ViewModel subscribe to the Model's PropertyChanged event
The INotifyPropertyChanged interface is meant to be used for any object that needs to raise property change notifications, not just objects that get bound to the UI. I use it all the time for non-UI purposes :)

Related

Do I have to Raise PropertyChanged in the ViewModel if I use EntityFramework?

I want to use MVVM pattern to develop a WPF application, the Model is an entityframework model. since entityframwork Implements INotifyPropertyChanged and raises PropertyChanged event in each property setter, do I have to raise this event in the viewmodel properties (wrappers of the model properties)?
You need to raise the PropertyChanged event for all properties you are binding to in your view (XAML) otherwise they won't know when they are supposed to update.
Therefore, if you are wrapping the model properties in the view model you will need to raise the event.
However, you can bind to the model properties directly:
<TextBox Text="{Binding Model.Property}" />
and as these properties implement INotifyChanged the UI will get notified and so will update. Therefore you don't need to wrap them at all.
Yes, you do.
UI components are bound to your properties, and your properties use EF entity objects. So you can catch changes in entity properties, but how the UI should now about the change? Answer: Your view model implements INotifyPropertyChanged and raises PropertyChanged event.

List<> collection does not update the View in MVVM

I used the List<Person> collection as the ItemsSource for the DataGrid control.
But it did not update the View if i remove the item from the List collection. I was struggling for a long time for the solution.
Then Instead of the List<Person> collection in my ViewModel. I changed this into ObservableCollection<Person> collection. Now it updates the view when ever there is a change in the collection.
I am not sure why it updates only for ObservableCollection<Person> ? Anyone ?
Well its in the name. A simple List doesn't tell the ui to update, in other words "the view can't observe the list". There is no weird magic behind Databinding. WPF and DataBinding needs the Data model to tell them "this is new" or "this is changed", you propably already saw INotifyPropertyChanged, INotifyCollectionChanged is the same but for collections, and the List<> doesn't implement it, ObservableCollection does.
Because List does not implement INotifyCollectionChanged
Because the update of a databinding is not a kind of magic, there are several requirements to make databinding working correctly. If you have a single property to bind on this property must be either a dependency property or its parent class must implement the INotifyPropertyChanged interface to notify the wpf binding system about changes of the property value.
For a collection there is a simelar mechanism: it must implement INotifyPropertyChanged to inform the wpf binding system about removed/moved/added items.
See here for more details: http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx
ObservableCollection<T> fires change events every time you change the items in the collection. List<T> doesn't. That's the reason.
DataBinding is lazy. If you don't tell your view that something has changed, it won't bother updating. Under the hoods, WPF DataBinding registers for change notifications, so that your ViewModel can tell the view when it has changed. It does this with interfaces like INotifyPropertyChanged and INotifyCollectionChanged.
ObservableCollection<T> implements the interface INotifyCollectionChanged. This interface defines the event CollectionChanged, which your View basically attaches its own event handler to. That handler will update the view when the event is raised by the collection.

How to implement INotifyPropertyChanged and observableCollection in MVVM pattern?

I have an ObservableCollection of Products in the Model, and I want the ViewModel to listen to any changes in the ObservableCollection of Products.
Im not sure about how to go and implement it. Ive read several tutorials, but most of them are not MVVM specific.
Should i implement the INotifyPropertyChanged in the ViewModel class, how do I specify that i want to listen to the OberserableCollection of Products?
Thanks :)
I know this has already been answered, but there is more to consider.
1) a ViewModel should implement INotifyPropertyChanged, that's one of the things that makes it a ViewModel. Even in the unlikely case that the only property it exposes is the ObservableCollection, it will need to raise property changed when the actual ObservableColelction property is changed (more on this in item 3)
2) do you really want the ViewModel to listen for these changes or the View? Those are two different things. The ViewModel should hold an ObservableCollection which is then bound to the View. What you want is for the View to react to those changes. In that case, Brandon is correct that ObservableCollection does this for you out of the box. So the View has an instance of the ViewModel set as a DataContext and some visual element in your View is bound to the ObservableCollection (like an ItemsSource on a ListBox).
3) the exception is the ObservableCollection property itself in the ViewModel. While ObservableCollection implements INotifyPropertyChanged, that is part of the collection object: when that object reference changes (like it is recreated) in the ViewModel, then the ViewModel still needs to report that the Property for the ObservableCollection has changed.
Just some thoughts.
ObservableCollection already implements INotifyPropertyChanged, so you don't need to.

WPF MVVM: Notify property changed one property everytime some other property is changed

I have a ViewModelBase class where I define RaisePropertyChanged method for the INotifyPropertyChanged interface. Most MVVM people are pretty familiar with this.
I also have a Validating ViewModel that inherits from ViewModelBase. It implements a interface and some methods to validate it's properties. It has a IsValid property the is only a getter who checks if there are any rule violations. However if I would wan't to bind to this property in the Views it would have to get updated at some time. This would basicly be everytime some other property is changed, or at least properties that I validate against. One simple solution would be to just Notify IsValid inside the RaisePropertyChanged method but it is defined in ViewModelBase where IsValid hasn't been defined.
Would any of you have a good solution for this dilemma?
If you are following the standard event handling pattern, your RaisePropertyChanged method will be marked as virtual (it should probably be a protected virtual method), so you'll be able to override that method in your validating view model, call the base implementation, and then invoke the PropertyChanged event for the IsValid property.
I'm not quite sure what you are doing, but if it is general validation, you would be better off using a framework such as DataAnnotations.

WPF Binding to ObjectDataProvider Method and detecting return value dependencies

I'm binding to a method using an ObjectDataProvider. The class which exposes this method contains an ObservableCollection of type T:INofifyChanged. My problem is that because the methods return value is dependent upon the value of it's ObservableCollection, i need the binding to be updated when the ObservableCollection changes in any way.
In short, the return value of the method is dependent on other factors and i want this method binding to refresh when it's dependencies change.
How can i let the bound control know when the methods return value will be different?
The ObservableCollection class exposes the CollectionChanged event which you could hook into.
You would possibly be better off using a ViewModel and handling the update via this though. Take a look at the MVVM pattern.

Resources