mortenstarck
mortenstarck

Reputation: 2803

Set Text on combobox while getting data

I'm trying to display a default text in a ComboBox while I'm fetching data from a source, but it doesn't show anything.

<ComboBox  
         Grid.Row="1" 
         Grid.Column="2" 
         Text="Hepper"
         ItemsSource="{Binding Builds}"
         SelectedItem="{Binding SelectedBuild}"
         DisplayMemberPath="VersionNo" 
         IsReadOnly="True" 
         IsEnabled="{Binding SelectedBuildEnable}" 
         VerticalAlignment="Top" 
         HorizontalAlignment="Left" 
         Width="180" 
         Height="30" 
         MinWidth="180" />

Upvotes: 0

Views: 79

Answers (2)

Herm
Herm

Reputation: 2989

you can try to set the ComboBox.SelectedValue Property instead of ComboBox.Text.

I prefer to show another TextBlock above the ComboBox to display a default text:

<!-- don't forget to define the converter in your resources -->
<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>    

<!-- your Control -->
<ComboBox  
     Grid.Row="1" 
     Grid.Column="2" 
     x:Name="ComboBoxElement"
     ItemsSource="{Binding Builds}"
     SelectedItem="{Binding SelectedBuild}"
     DisplayMemberPath="VersionNo" 
     IsReadOnly="True" 
     IsEnabled="{Binding SelectedBuildEnable}" 
     VerticalAlignment="Top" 
     HorizontalAlignment="Left" 
     Width="180" 
     Height="30" 
     MinWidth="180" />

<TextBlock 
      Grid.Row="1" 
      Grid.Column="2" 
      Visibility="{Binding IsEnabled, ElementName=ComboBoxElement, Converter={StaticResource BooleanToVisibilityConverter}}" 
      IsHitTestVisible="False" 
      Text="Hepper" 
      VerticalAlignment="Top" 
      HorizontalAlignment="Left" 
      Margin="15,5" />

I guessed that your ComboBox becomes enabled if the data is fetched. Otherwise you have to use another binding for the visibility.

Upvotes: 3

Sheridan
Sheridan

Reputation: 69959

According to MSDN, the ComboBox.Text property

Gets or sets the text of the currently selected item.

Therefore, you could temporarily add an item into your ComboBox with your required message, select it and then remove it before filling the ComboBox when your data arrives.

Upvotes: 0

Related Questions