Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is CompositeCollection not Freezable?
    text
    copied!<p>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.</p> <p>Recently, I tried to implement a ComboBox with an "extra" element beyond the collection my ViewModel provides that says "Select Item".</p> <pre><code>&lt;ComboBox&gt; &lt;ComboBox.ItemsSource&gt; &lt;CompositeCollection&gt; &lt;ComboBoxItem IsEnabled="False"&gt;Select Item&lt;/ComboBoxItem&gt; &lt;CollectionContainer Collection="{Binding MyItemsCollection}" /&gt; &lt;/CompositeCollection&gt; &lt;/ComboBox.ItemsSource&gt; &lt;/ComboBox&gt; </code></pre> <p>The problem is, CompositeCollection is not a Freezable: <a href="http://msdn.microsoft.com/en-us/library/ms750509.aspx" rel="noreferrer">Freezable Objects Overview</a>. This causes only the static ComboBoxItem to appear and none of the results from my binding expression.</p> <p>My initial reaction to the problem was to just implement my own version of CompositeCollection that <em>was</em> Freezable. This, though, begs the following question:</p> <p><strong>Why isn't CompositeCollection a Freezable in the first place?</strong></p> <p>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.</p> <p>Any help would be appreciated. Thanks!</p> <p><em>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.</em></p> <p><strong>Update:</strong></p> <p>After some further research brought on by ArsenMkrt's comment, I'm led to believe this was actually an oversight. The evidence is this:</p> <ol> <li>There is a collection that <em>is</em> freezable called <code>FreezableCollection&lt;T&gt;</code>. I does not produce CollectionViews, which makes it inappropriate for my needs directly.</li> <li>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.</li> </ol> <p>My current plan to get around this problem is to create a new collection with the properties of CompositeCollection and <code>FreezableCollection&lt;T&gt;</code>. I don't know if it'll work yet, but I'm thinking about something like this:</p> <pre><code>public class BindableCompositeCollection : FreezableCollection&lt;object&gt;, ICollectionViewFactory </code></pre> <p>If anyone has a better option, I'd like to hear it!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload