24

I have a ViewModel class which has large number of properties(Say 50). Once the data is set on to all the properties I need to update the UI. I know that the common solution is to raise PropertyChanged on all the property setters.

I am wondering if there is any way I can notify my DataTemplate to update all its bindings through a single notification? One idea is to have a IsLoaded property which raises the propertychanged but how can I use that to update the entire DataTemplate. I am interested in a total XAML solution to this.

2 Answers 2

50

Finally I have got an answer from my colleague Josh Smith , it is very simple, we just need to raise a PropertyChanged event with null or String.Empty as property name. which will tell the WPF binding system to re-evaluate all the bindings of that object. I am getting two major advantage while using this.

  1. The overhead of raising individual events on each property will reduce to just one Event from View-Model to the UI. Performance will get increased.
  2. While writing code we can just use {get;set;} syntax which is clean and less writing

Assumption : As in the question I assumed here that it is a very special condition in which all my Properties are getting updated at the same time

I have a blogpost discussing this here

Update: based on the comment from kek444

1
  • 10
    From msdn: "The PropertyChanged event can indicate all properties on the object have changed by using either using a null reference (Nothing in Visual Basic) or String.Empty as the property name in the PropertyChangedEventArgs." First line and I'm guessing almost everybody (including myself) missed it - doh! Jul 17, 2009 at 21:57
0

I don't think this a problem worth trying to solve. That is, I think the solution here is to have the individual properties send property-changed notifications as they are set. If not, what are you going to do when an individual property (but not necessarily all fifty) changes?

4
  • Imagine the situation of loading a bunch of data(All properties of a DO)from a Webservice, The DataTemplate was showing an animation until the entire data has been loaded, I need to update them all in one event firing.. In this question I assume there is no individual Set happening
    – Jobi Joy
    Jul 16, 2009 at 2:16
  • Bind the display of the animation to an IsLoading property in your ViewModel and have the IsLoading property set to true until all the data is loaded. The individual properties can still fire property-changed notifications.
    – jason
    Jul 16, 2009 at 2:56
  • Yeah that is the obvious MVVM solution But what I am looking for is something to refresh the DataContext of Template so that it will show all data with out a VM burden
    – Jobi Joy
    Jul 16, 2009 at 4:08
  • 2
    I can think of one very common scenario where you would need a notification like this. If the user chose to cancel an edit, you would want to revert the state of your ViewModel to the original values and notify the View that multiple properties have been reset.
    – dthrasher
    May 2, 2010 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.