37

Hi I have a WPF Combobox which shows a list of Enums. Code is below.

    <ComboBox HorizontalAlignment="Left" 
              Margin="139,299,0,0" 
              VerticalAlignment="Top" 
              ItemsSource="{Binding Source={StaticResource Enum}}"
              Width="78"/> 

However, when the view is loaded, it shows the first enum in the list, but I would like it to show 'Please Select', so is there XAML to do this (C# in the view if needs be..)

Thanks

1

7 Answers 7

54

All good answers that has been supplied, but I used the following to solve my problem

<ComboBox SelectedIndex="0">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ListBoxItem>Please Select</ListBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

Thanks for everyone who has helped!

3
  • 1
    This is quite a nice and smart solution. Aug 28, 2016 at 10:07
  • you can use ComboBoxItem Visibility="Collapsed">Please Select</ComboBoxItem> this will make the selection list render nicely
    – IronHide
    Dec 7, 2017 at 8:31
  • 3
    Good approach, but the issue is that the SelectedItem or SelectedValue will be "System.Windows.Controls.ComboBoxItem: Please Select" Jan 22, 2018 at 6:06
23

Add these properties to your combobox and you can set a default 'Please Select' Text on a combobox.

<ComboBox IsEditable="True" IsReadOnly="True" Text="Please Select"/>

For a more versatile solution you can create a watermark for the combobox

2
  • 1
    i know this is old, but i'm having issues with this. it would show "please select" but once i select an item, it would show the class / datatype of the combobox instead of the property i want displayed
    – gdubs
    Oct 4, 2018 at 6:43
  • @gdubs If you have another question, please ask it by clicking the Ask Question button.
    – Hille
    Apr 8, 2019 at 6:36
14

I did this with mine, works for me, since I have static items.

<ComboBox Name="cbxType" HorizontalAlignment="Left" Margin="116,41,0,0" VerticalAlignment="Top" Width="192">
    <ComboBoxItem Name="create" IsSelected="True">create database</ComboBoxItem>
    <ComboBoxItem Name="update">update database</ComboBoxItem>
</ComboBox>
10

You could achieve that with the following code:

<Grid>
                <ComboBox
                    MinWidth="120"
                    x:Name="MyCombo"
                    ItemsSource="{Binding FileTypes}"  
                    SelectedItem="{Binding SelectedFileType}"/>
                <TextBlock
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Visibility="{Binding SelectedItem, ElementName=MyCombo, Converter={StaticResource NullToVisibilityConverter}}"
                    IsHitTestVisible="False"
                    Text="Select Option...  " />
</Grid>

Whenever you need the above text (the textbox) you can use the VisibilityConverter to display your text on top of the combobox...

Add something like this to your resources:

<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
4

Add the value "Please select" to your EnumCollection

Set the default value in the combobox stylesetter

<Style x:Key="ComboStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="SelectedIndex" Value="0"/>
</Style>

XAML:

<ComboBox HorizontalAlignment="Left" 
              Margin="139,299,0,0" 
              Style="{StaticResource ComboStyle}"
              VerticalAlignment="Top" 
              ItemsSource="{Binding Source={StaticResource ComboBox}}"
              Width="78"/> 
3
  • Thanks, looks sensible, however, the Enum comes from the database so I don't particularly want to add 'Please Select' to it. Is there a way around it? Apr 17, 2014 at 10:39
  • @user3428422 you can check this thread stackoverflow.com/questions/4639533/default-value-for-combobox Apr 17, 2014 at 10:40
  • 2
    I think this is a poor solution. Changing the data source to change the behaviour of its presenter is a real code smell. Krishna's answer is way better as it only involves changes in the control.
    – ProfK
    Feb 1, 2017 at 7:15
4

Set the default value of the ComboBox to "SELL" when the control is first loaded/initialized in a WPF window/user control:

<ComboBox x:Name="OrderType" 
          Width="100" Height="20"
          SelectedIndex="1">
         <ComboBoxItem Content="BUY"/>
         <ComboBoxItem Content="SELL"/> 
</ComboBox>
2

Don't know how to do it without code-behind, maybe some triggers or DataTemplateSelectors...?

In code-behind:

  1. Add enumerable string property which will contain only one string: "Please select"
  2. In XAML set ItemsSource to that property and SelectedIndex = 0
  3. In DropDownOpened event set ComboBox.ItemsSource to your Enum collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.