Roberto Salemi

Roberto Salemi

  • NA
  • 146
  • 136.6k

[WPF] ComboBox and ItemSource: add one empty row

Feb 27 2015 6:26 AM
Hi,

I have a ComboBox, defined as:

<ComboBox Name="cmbTypeOfSchedule"
DataContext="{StaticResource DomainDataViewModel}"
ItemsSource="{Binding SchedTypes}"
DisplayMemberPath="Description"
Width="120">


I would insert, in head, an empty item or empty called "Select all".

So:

1) I defined a StaticResource:

<CollectionViewSource x:Key="SchedTypesWithEmptyItem" Source="{Binding SchedTypes}" />
2) Edit my Combobox as:

                            <ComboBox Name="cmbTypeOfSchedule"
                                        DataContext="{StaticResource DomainDataViewModel}"
                                        DisplayMemberPath="Description"
                                        Width="120">
                                <ComboBox.ItemsSource>
                                    <CompositeCollection>
                                        <ComboBoxItem Content="Select all" />
                                        <CollectionContainer Collection="{Binding Source={StaticResource SchedTypesWithEmptyItem}}" />
                                    </CompositeCollection>
                                </ComboBox.ItemsSource>

The problem: in the ComboBox I see only "Select all" and not other item from my Collection and when I select "Select all", it does not stay selected.

Thanks.


Answers (1)