ComboBox Text

When you want to implement a required ComboBox, but don’t want to default the selection, the initial state of the ComboBox is empty.

How to display a nice message like “Please Select a Widget”?

I always start by setting the Text property, then rub my head as to why it isn’t displayed! The reason is that there is two ControlTemplates used by ComboBox; an Editable one and a non-Editable one. Only the Editable template uses the Text property.

Once you know this small tidbit, the rest is easy. Set IsEditable to true, Set IsReadOnly to True and set your text.

<ComboBox Text="Select An Item or Else!" IsEditable="True" IsReadOnly="True">
       <ComboBoxItem Content="Test 1"/>
       <ComboBoxItem Content="Test 2"/>
</ComboBox>

Note: if you are using a custom ItemTemplate, the selected item will not appear correctly. You should then set IsEditable to true in a trigger when there is no SelectedItem.

<Style TargetType="ComboBox">
  <Style.Triggers>
    <Trigger Property="SelectedItem" Value="{x:Null}">
	<Setter Property="IsEditable" Value="True" />
    </Trigger>
  </Style.Triggers>					
</Style>
Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: