38

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

user3428422
  • 4,300
  • 12
  • 55
  • 119
  • check this http://stackoverflow.com/questions/2901536/can-a-wpf-combobox-display-alternative-text-when-its-selection-is-null – ElectricRouge Apr 17 '14 at 10:36

7 Answers7

57

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!

almulo
  • 4,918
  • 1
  • 20
  • 29
user3428422
  • 4,300
  • 12
  • 55
  • 119
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

Coden
  • 2,579
  • 1
  • 18
  • 25
Krishna
  • 1,956
  • 13
  • 25
  • 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 04 '18 at 06:43
  • @gdubs If you have another question, please ask it by clicking the [Ask Question](//stackoverflow.com/questions/ask) button. – Hille Apr 08 '19 at 06:36
15

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>
MelloG
  • 1,044
  • 1
  • 11
  • 11
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" />
AP_TheMoe
  • 111
  • 3
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>
Bo Jangles
  • 51
  • 3
3

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"/> 
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • 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? – user3428422 Apr 17 '14 at 10:39
  • @user3428422 you can check this thread http://stackoverflow.com/questions/4639533/default-value-for-combobox – Sajeetharan Apr 17 '14 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 01 '17 at 07:15
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
amnezjak
  • 2,011
  • 1
  • 14
  • 18