I am writing an application using the MVVM pattern. I am providing data to my view by setting my view's DataContext property to an instance of my ViewModel. Generally I just use Binding from there and go about my way.
Recently, I tried to implement a ComboBox with an "extra" element beyond the collection my ViewModel provides that says "Select Item".
<ComboBox>
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="False">Select Item</ComboBoxItem>
<CollectionContainer Collection="{Binding MyItemsCollection}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
The problem is, CompositeCollection is not a Freezable: Freezable Objects Overview. This causes only the static ComboBoxItem to appear and none of the results from my binding expression.
My initial reaction to the problem was to just implement my own version of CompositeCollection that was Freezable. This, though, begs the following question:
Why isn't CompositeCollection a Freezable in the first place?
My concern is that generally these decisions are made for a reason and I don't feel I know enough about Freezable to say why they didn't inherit from it. I know I can implement this collection, but I'm concerned there will be a measurable difference in performance if I do.
Any help would be appreciated. Thanks!
Also: please note that I realize I can insert a Null or some other special value and provide and template or valueconverter to do what I want. This is not the question I'm interested in... only the question in bold above.
Update:
After some further research brought on by ArsenMkrt's comment, I'm led to believe this was actually an oversight. The evidence is this:
- There is a collection that is freezable called
FreezableCollection<T>
. I does not produce CollectionViews, which makes it inappropriate for my needs directly. - Sam Bent of MSFT says as much in the above link. I cannot find contact information for him yet, but I plan on discussing this with him if I get the chance.
My current plan to get around this problem is to create a new collection with the properties of CompositeCollection and FreezableCollection<T>
. I don't know if it'll work yet, but I'm thinking about something like this:
public class BindableCompositeCollection : FreezableCollection<object>, ICollectionViewFactory
If anyone has a better option, I'd like to hear it!