Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Combobox DefaultValue (Please Select)

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

like image 828
user3428422 Avatar asked Apr 17 '14 10:04

user3428422


2 Answers

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!

like image 163
user3428422 Avatar answered Oct 06 '22 01:10

user3428422


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

like image 34
Krishna Avatar answered Oct 06 '22 01:10

Krishna