Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combobox with static and dynamic data

I would like to create a combobox that binds to a dynamic resource and has a custom xaml pre-specified list that stays at the top. I know how to bind a combobox to a dynamic resource,

<ComboBox Name="comboBox1" Width="Auto" ItemsSource="{Binding}" />

and I know how to insert static items (see WPF - add static items to a combo box ).

<ComboBox Text="Is not open"> 
  <ComboBoxItem Name="cbi1">Item1</ComboBoxItem>
  <ComboBoxItem Name="cbi2">Item2</ComboBoxItem>     
  <ComboBoxItem Name="cbi3">Item3</ComboBoxItem> 
</ComboBox> 

but I don't know how to do both at the same time?

Note: obviously, there are plenty of different ways to do this with custom widgets and the like, I just feel this should be quite easy somehow.

like image 205
saltyseadog Avatar asked Oct 03 '12 16:10

saltyseadog


1 Answers

CompositeCollection is pretty cool for this. Something like so:

<ComboBox>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding Source={...whatever...}" />
            <ComboBoxItem Name="cbi1">Item1</ComboBoxItem>
            <ComboBoxItem Name="cbi2">Item2</ComboBoxItem>
            <ComboBoxItem Name="cbi3">Item3</ComboBoxItem>
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>
like image 192
dtm Avatar answered Oct 11 '22 10:10

dtm