简体   繁体   中英

Combo box item not initally selected

I need my ComboBox's first item to be selected when loaded.

XAML:

 <ComboBox x:Name="CreateLstBoxFormat" HorizontalAlignment="Left" Margin="27,334,0,0" IsSynchronizedWithCurrentItem="True" VerticalAlignment="Top" Width="90"  SelectedValuePath="Content" SelectedIndex="0" SelectedItem="{Binding CreateFormatSelectedItem, Mode=TwoWay}">
    <ComboBox.ItemsSource>
        <x:Array xmlns:sys="clr-namespace:System;assembly=mscorlib" Type="{x:Type sys:String}">
            <sys:String>MXF</sys:String>
            <sys:String>Quicktime MOV</sys:String>
            <sys:String>DPX</sys:String>
            <sys:String>TIF</sys:String>
            <sys:String>TGA</sys:String>
            <sys:String>CIN</sys:String>
            <sys:String>EXR</sys:String>
        </x:Array>
    </ComboBox.ItemsSource>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding Create_FormatSelectCommand}" 
                                    CommandParameter="{Binding YourCommandParameter}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

I expected MXF to be initially selected in the combo box on opening of the window.

I'm using the SelectedItem property to fire an event when the selected item has changed.

Try using SelectedItem property:

<ComboBox Name="myComboBox" 
          ItemsSource="{Binding}" 
          DisplayMemberPath="Description" 
          SelectedItem="{Binding Path=id}"
          IsSynchronizedWithCurrentItem="True"
          SelectedIndex="0" />

I think using

SelectedIndex="0(or your desired index)"

will do that for you, even if you are working with MVVM approach. if you don't select anything then it will always show it as blank initially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM