5

I have an ObservableCollection that's binded to a WPF ListView, and all the values appear correct. But how can I get a notification when something that has a 2-way binding changes?

Should I use INotifyPropertyChanged just like in Winforms? Or are there better practices to do these?

I saw some people suggesting using dependency properties online, but not sure if that's what should do.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

4 Answers4

4

If the class I want to add a property is a DependencyObject, I generally add a DependencyProperty. If the class is a POCO (plain old clr objects), then I implement INotifyPropertyChanged.

In generall, all my business objects are POCOs and therefore I use INotifyPropertyChanged. In the WPF world, I mostly use DependencyObjects (view models, custom controls, UserControls...) and therefore they are DependencyProperties. An exception are ViewModels representing items (to be used as items in an items source). In this case I think DependencyProperties are not very practical (Because Equals() and GetHashCode() of DependencyObjects are sealed and DependencyObject is thread-dependent).

If your class already is a DependencyObject, using DependencyProperties may give you some nice advantages: You don't have to back every value, a powerfull inheritance system, default-values, property changed callbacks per property, value coercion ... (I like DependencyProperties probably more than other people them like :)

Conclusion: Based on the title of your question: How to get notified when something changes in a WPF window?, my way would be to add a DependencyProperty and not a clr-property because the Window is a DependencyObject. By the way, Visual Studio has a nice Snippet to create DependencyProperties.

HCL
  • 36,053
  • 27
  • 163
  • 213
  • On issue with using dependency properties on your business objects is that you're forced to create them on the UI thread. You can only use a dependency property on the thread that creates them. – SergioL Mar 03 '11 at 16:38
  • @SergioL: This is why I wrote the passage about the ViewModels representing items. I create them often asynchronous and therefore DependencyObjects are unhandy (Besides from the sealead Equals() and Hashcode(), this can also be very annoying). However for UI-Controls, this is no problem because they have to be created anyway from the UI-thread. But agree, my english is not very good and perhaps I'm not very clear with my explanation. Im working on it (my english).. :) – HCL Mar 03 '11 at 16:52
2

There are really two scenarios:

1) Notifying the UI when a piece of data changes-either in the ViewModel or a Model that is bound to some UI elements (usually due to a data binding). In this scenario you'd use INotifyPropertyChanged.

Example: you have a Person object that binds it's Name to a TextBox. Person needs to implement INotifyPropertyChanged and the appropriate code needs to be attached to the Name Property's setter.

2) Creating custom UI elements where you'd want data to be bound. In this case, you'd use custom dependency properties.

Example: you have a custom UI element that spins when a certain condition is met. So you create an IsSpinning dependency property that you can bind to your ViewModel's IsLoading property. The custom control then uses the IsSpinning property to modify its behavior.

I hope that was clear...

SergioL
  • 4,905
  • 3
  • 26
  • 30
1

You are right. You can use INotifyPropertyChanged or you can implement dependency properties. Personally I prefer INotifyPropertyChanged, it's more light and easyer to implement than dependency property.

Davita
  • 8,928
  • 14
  • 67
  • 119
  • Thanks appreciate your answer. What's the use of dependency properties? Are they more powerful than INotifyPropertyChanged? – Joan Venge Mar 02 '11 at 21:54
  • 1
    As far as I know, there's no much diference. DP has some restrictions. take a look http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4c5a100b-fe27-43a0-b04e-c08492f03200/ and here http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel – Davita Mar 02 '11 at 21:55
1

I've generally used INotifyPropertyChanged for my data objects.

DependencyProperties can only be implemented on framework elements I believe.

Edit: I take that back... DependencyProperties can only be implemented on DependencyObjects (A FrameworkElement is an example of that as it derives from UIElement which derives from Visual which derives from DependencyObject).

If you have a UserControl, you may wish to implement a Dependency Property so you can Bind that Property (in XAML) to some other property. You could not do this scenario with INotifyPropertyChanged.

Scott
  • 11,840
  • 6
  • 47
  • 54
  • The last paragraph is not true. You can bind clr-properties from xaml the same way as DependencyProperties. The binding engine supports both types. – HCL Mar 02 '11 at 23:02
  • @HCL: You can bind an existing control's dependency property to an object's propery implementing INotifyPropertyChanged... yes. But If you have a UserControl (MyUserControl) with a CLR Color property called SecondaryBackground, you could not declare But if SecondaryBackground were a DependencyProperty then you could. – Scott Mar 02 '11 at 23:09
  • Your example is not about binding but assigning values. However also this is possible. I don't see any problem with this. The two things you have to take care about is to specify the namespace also for the property-declaration and to take care with the desired data-type. But anyway, as I already mentioned, for a DependencyObject I propose to use DependencyProperties :) – HCL Mar 02 '11 at 23:23