Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF bound ComboBox with CompositeCollection - setting SelectedItem
    primarykey
    data
    text
    <p>I have a bound WPF comboBox that has an ItemsSource set to a CompositeCollection. I'm trying this to try and accomodate adding &lt;Select> and &lt;Add New...> selections to precede an ObservableCollection of 'regular' objects. What I can't figure out is how to, in code-behind, select one of these added choices.</p> <p>This is how I'm building the CompositeCollection:</p> <pre><code>private CompositeCollection CreateItemsSource(ObservableCollection&lt;T&gt; source) { CompositeCollection cmpc = new CompositeCollection(); cmpc.Add(new ComboBoxItem { Content = "&lt;Select&gt;" }); cmpc.Add(new ComboBoxItem { Content = "&lt;Add New...&gt;" }); var cc1 = new CollectionContainer { Collection = source }; cmpc.Add(cc1); return cmpc; } </code></pre> <p>This is what the ComboBox looks like:</p> <pre><code>&lt;DataTemplate x:Key="LookupComboTemplate"&gt; &lt;TextBlock Text="{Binding}"/&gt; &lt;/DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding SubCategories.ItemsSource}" ItemTemplate="{StaticResource LookupComboTemplate}"&gt; &lt;ComboBox.SelectedItem&gt; &lt;Binding Path="SourceData.SubCategoryObj" Mode="TwoWay"&gt;&lt;/Binding&gt; &lt;/ComboBox.SelectedItem&gt; &lt;/ComboBox&gt; </code></pre> <p>I've got a situation where SelectedItem SourceData.SubCategoryObj is null (it's an optional property). In this case, I want to manually select and display the &lt;Select> choice. But no matter what I do (setting SelectedIndex is ignored, setting SelectedValue to the ComboBoxItem in the CompositeCollection is ignored) I get a blank ComboBox when it renders.</p> <p>I'd appreciate any advice on how I can do this.</p> <p>Thanks! Corey.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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