WPF Combobox DefaultValue (Please Select)
Solution 1
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!
Solution 2
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
Solution 3
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>
Solution 4
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" />
Solution 5
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"/>
Comments
-
user3428422 almost 4 years
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