views:

107

answers:

1

I'm trying to implement a property changed event (or which ever is appropriate) in my wpf project and I'm trying to find the best way to do this.

I have this header where it has an expand/collapse icon and in my window, I might have multiple headers. I want the default to be collapsed when all headers are listed in the beginning but when the user expands some of the headers (not all), when I click 'refresh' on my window to update the headers, I want it to stay the way it was before it was refreshed.

So, if the header 1 and 2 were expanded, and 3 and 4 were collapsed, when the refresh button is pressed, I want it to actually remember that. The problem is, when I click 'refresh' it refreshes the whole view so it loses the information.

I'm trying to have a collection in the actual class that hosts these headers to store the information so when a header is expanded or collapsed, it will notify the collection that a expanded/collapsed has fired and I want to update that value within the collection so when the refresh button is clicked, I can compare it with a GUID if the header was already present in the view before the refresh, update the new header coming in with the stored information of expand/collapse so even after the refresh, it will hold it's state.

+1  A: 

For the individual instances of the classes that are bound to, you want to implement the INotifyPropertyChanged interface.

Also, on the collection that the headers are stored in, you will want to implement the INotifyCollectionChanged interface to indicate when the collection itself changes.

These two interfaces, if implemented correctly, should enable your objects (and the collections they are in) to be bound to correctly by the WPF databinding system.

casperOne
for INotifyCollectionChanged check out ObservableCollection<T>
Aaron Hoffman
Hey casperOne, Thank you so much for the quick reply.I've been thinking, is it possible to use INotifyPropertyChanged interface when the boolean property I have is not even present in the .xaml? A user clicks on a '[+]' to expand and the icon changes to '[-]' collapse and when the header is expanded, I am trying to set the 'IsExpanded' property to TRUE and everytime this property changes, I want to notify to the class that hosts the headers (not notifying to any view). Thank you!
@Dan: You could, but if it's not in the XAML then you aren't going to be able to bind to the value.It also implies that your design is a little unclean, in that you don't have this property exposed (and you should have it exposed separately from the data that the header is for).
casperOne