组合框项目未初始选择

时间:2015-07-16 10:11:17

标签: c# wpf mvvm combobox

我需要在加载时选择我的ComboBox的第一个项目。

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>

我希望在打开窗口时最初在组合框中选择MXF

我使用SelectedItem属性在所选项目发生变化时触发事件。

2 个答案:

答案 0 :(得分:1)

尝试使用SelectedItem属性:

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

答案 1 :(得分:0)

我认为使用

SelectedIndex="0(or your desired index)"
即使您正在使用MVVM方法,

也会为您做到这一点。如果你没有选择任何东西,那么它最初总会显示为空白。

相关问题