Parrish

Can you use an ObservableCollection objects as the type of a DependencyProperty Using this code below cause an error. The GetValue method returns nothing.

Public Shared NickNamesProperty As DependencyProperty = _

DependencyProperty.Register("NickNames", GetType(ObservableCollection(Of String)), GetType(Institutional))

Public Property NickNames() As ObservableCollection(Of Address)

Get

Return GetValue(NickNamesProperty)

End Get

Set(ByVal value As ObservableCollection(Of Address))

SetValue(NickNamesProperty, value)

End Set

End Property



Re: Windows Presentation Foundation (WPF) ObservableCollection used as a DependencyProperty

Sam Bent - MSFT

If you haven't set the value of the property, it will have its default value, namely "nothing".

This should work, but of course you have to assign an interesting value to the property before you can get an interesting value out of it.

There are some problems with collection-valued properties (even regular properties, not DPs). It's usually best to make these read-only properties whose value is a collection object that is initially empty. You then use the property value by calling methods like Add and Remove. So the property always returns the same collection object, but that collection can grow and shrink as you add/remove elements to it. If you make the property writeable, you allow people to replace your collection object by another collection object. This is often very confusing, especially if the collection object exposes events like CollectionChanged - replacing the collection object leaves all the listeners to the old collection's events out of the loop when you make changes to the new collection.

This is easy enough to do for normal properties: just declare a getter, but no setter, and initialize the property to an empty collection in the constructor. For a DP though, this isn't enough - or rather I should say that the "obvious" thing to do doesn't work. You can't (or shouldn't) define the DP's default value to be an empty collection object, because that object will be shared as the value of the property on each instance of the class that defines the property. [In your example, each instance of Institutional will share the same collection object as the value of its Nicknames property.] So adding something to one Institional's Nicknames will add it to all Institutional's Nicknames. Not what you want!

You can get the right effect by declaring your DP as read-only (read about RegisterReadOnly and DependencyPropertyKey). In the constructor for the owning class (Institutional), use the read-only key to initialize the property to a new empty collection object. Like this (pardon my C#, I'm no good at VB):

public Institutional()

{

ObservableCollection<Address> value = new ObservableCollection<Address>();

SetValue(NicknamesPropertyKey, value);

}






Re: Windows Presentation Foundation (WPF) ObservableCollection used as a DependencyProperty

David Cater

Thanks for a great explanation of a subject I've found more than a little confusing in the past.

Now I'm hoping you have an answer to my next question. Once you have a read-only empty collection as a DependencyProperty that you've initialized from the constructor, is there any way to add elements to that collection from a style setter

I'm fairly certain the answer is no. I'm pretty sure that a setter can really only be used to create new objects (assigning a new instance of a collection to your Nicknames dependency property if it weren't read-only), and that there is no way to use that architecture to add items to an existing collection.

What I'm trying to do is create a custom data grid class that allows the user of the class to add columns to the class from within a style setter. I think I'm going to have to allow the user to instantiate a new collection to do this, similar to instantiating a new collection to assign to the ItemsSource property in an ItemsControl object. I would much rather have the Columns collection be read-only as you specified above, but I can't think of any way to let the user modify the collection from a style Setter in that case. Can you

Thanks for any insight you can provide,

David Cater





Re: Windows Presentation Foundation (WPF) ObservableCollection used as a DependencyProperty

Sam Bent - MSFT

Your fears are justified. There's no way to add items to a collection from a Setter.






Re: Windows Presentation Foundation (WPF) ObservableCollection used as a DependencyProperty

David Cater

Thanks for the response!

I would like to cast one vote for possibly including this kind of feature in a future version of WPF. Perhaps there could be an "<Adder>" in addition to a "<Setter>".

I would think there would be some pretty clear symmetries between editing collections in this way and editing collections in the config files for ASP.NET. For example, perhaps there would be a way to include support for "<add>", "<clear>" and "<remove>" tags for editing collections in styles (including support for cumulative manipulation of collections as styles are derived from other styles using BasedOn).

Thanks again,

David Cater