Search code examples
wpfcomboboxdatatrigger

wpf combo box default value


I'm trying to default the Combo Box selected item to index = 0 when the SelectedValue is Null. What is wrong with the data trigger ? Error: SelectedIndex is not recognized property

 <ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}" 
                    DisplayMemberPath="TypeName" SelectedValuePath="TypeName" 
                    SelectedValue="{Binding  AnObj.Type, Mode=TwoWay}"  >
                        <ComboBox.Triggers>
                            <DataTrigger Binding="{Binding}" Value="{x:Null}">
                                <Setter Property="SelectedIndex" Value="0" />
                            </DataTrigger>
                        </ComboBox.Triggers>
                    </ComboBox>

Solution

  • You should do it by creating a style trigger like below

    <ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}" 
                    DisplayMemberPath="TypeName" SelectedValuePath="TypeName" 
                    SelectedValue="{Binding  AnObj.Type, Mode=TwoWay}"  >
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Null}">
                    <Setter Property="SelectedIndex" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox>
    

    The error says that it does not have a qualifying type name so by creating a style it apply to Combobox when you set the TargetType="ComboBox"


    <ComboBox x:Name="ACombobox" ItemsSource="{Binding AList}"
                DisplayMemberPath="TypeName" SelectedValuePath="TypeName" 
                SelectedValue="{Binding  AnObj.Type, Mode=TwoWay}"   >
      <ComboBox.Resources>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedItem}" Value="{x:Null}">
                    <Setter Property="SelectedIndex" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
      </ComboBox.Resources>
    </ComboBox>
    

    This works for me.


    Example with StaticResource

         <Window.Resources>
            <x:Array x:Key="StringList" Type="System:String">
                <System:String>Line 1</System:String>
                <System:String>Line 2</System:String>
                <System:String>Line 3</System:String>
                <System:String>Line 4</System:String>
           </x:Array>       
        </Window.Resources>
        <ComboBox ItemsSource="{StaticResource StringList}" >
          <ComboBox.Resources>
            <Style TargetType="ComboBox">
                <Style.Triggers>
                    <Trigger Property="SelectedItem" Value="{x:Null}">
                        <Setter Property="SelectedIndex" Value="0"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Resources>
      </ComboBox>