user235973457
user235973457

Reputation: 331

Display default text in combobox not working

I am using WPF. I have been battling to solve this problem. I tried to follow several advices from google but it didnt work for me. I cannot manage to default "--Select Car--" in the combobox. I have tried to put Text="--Select Cars--" IsEditable="True" IsReadOnly="True" which is not working. It still displays "Toyota" as default. Look below here:

<DockPanel xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <DockPanel.Resources>
        <x:Array Type="{x:Type sys:String}" x:Key="cmbCarListItems">
            <sys:String>Toyota</sys:String>
            <sys:String>Kia</sys:String>
            <sys:String>Audi</sys:String>
        </x:Array>
    </DockPanel.Resources>
    <ComboBox Name="cmbCarList" 
              Text="--Select Cars--" 
              IsEditable="True"
              Cursor="Hand" 
              IsSynchronizedWithCurrentItem="True" 
              ItemsSource="{StaticResource cmbCarListItems}" 
              SelectionChanged="cmbCarList_SelectionChanged">
    </ComboBox>
</DockPanel>

Your code help much appreciated!!

Upvotes: 1

Views: 1436

Answers (1)

meilke
meilke

Reputation: 3280

This was working for me:

<Grid>

    <Grid.Resources>
        <x:Array Type="{x:Type sys:String}" x:Key="cmbCarListItems">
            <sys:String>Toyota</sys:String>
            <sys:String>Kia</sys:String>
            <sys:String>Audi</sys:String>
        </x:Array>
    </Grid.Resources>

    <!-- Not using IsSynchronizedWithCurrentItem="True" -->
    <ComboBox Name="cmbCarList" 
              Text="--Select Cars--" 
              IsEditable="True"
              Cursor="Hand"
              ItemsSource="{StaticResource cmbCarListItems}"  
              SelectionChanged="cmbCarList_SelectionChanged">
    </ComboBox>

</Grid>

Upvotes: 2

Related Questions