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

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.

Related

Update WPF control when event fires

I have a class which monitors a log file. It will fire an event when a new line is added.
What is the proper way, to update multiple controls in WPF?
Keep in mind that I am new to WPF bindings.
You should learn about bindings and MVVM. In MVVM you can have your viewmodel class implement INotifyPropertyChanged allowing the view to be automatically updated when a binding property in viewmodel class is updated. In your case your viewmodel can subscribe to the fired event and update a property which in turn will update the view (controls).

How to react to model changes?

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 :)

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 - ObservableCollection PropertyChanged event?

I have an object which has a property of type ObservableCollection<bool>. It is bound to a list of checkboxes on a form using TwoWay bindings. I would like to add a PropertyChanged notification to this so that if certain values are selected, some other ones get automatically deselected. Is there a way to do this?
The ObservableCollection.PropertyChanged event doesn't get triggered when a value in the collection gets changed and I'm using the MVVM design pattern.
You would need to use your own class that implements the INotifyPropertyChanged interface. You won't be able to use bool, but you can have a single property in your class that is that bool that you want.

Resources