wpfcombobox

ComboBox with empty item?


Suppose we have a DataSource bind to a collection from Database. There is no null item of course. How to add a void item into a ComboBox, so that at first load user would see an empty string. I don't want to add a dummy/void object into the Collection. Optimally in XAML. Any proposals?


Solution

  • <ComboBox Name="myComboBox" Width="200" Background="White">    
        <ComboBox.ItemsSource>    
            <CompositeCollection>
               <ComboBoxItem IsEnabled="False" Foreground="Black">Select Item</ComboBoxItem>
               <CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />    
            </CompositeCollection>
        </ComboBox.ItemsSource>
    </ComboBox>
    

    EDIT

    As @surfen mentioned in comment, BindingProxy is workaround for the binding issue