绑定到String Array Resource时,Combobox选择的索引不起作用

时间:2018-01-12 02:31:12

标签: wpf xaml combobox binding

我将字符串数组资源绑定到combobox ItemsSource。当我说SelectedIndex = 0时,它不会选择任何东西。请帮忙。这只是代码的一部分。

xmlns:sys="clr-namespace:System;assembly=mscorlib"

<lib:MetroWindow.Resources>
        <converter:EnumToVisibilityConverter
            x:Key="EnumToVisibility"></converter:EnumToVisibilityConverter>

        <x:Array
            Type="{x:Type sys:String}"
            x:Key="ImageFormatsArray">
            <sys:String>Bmp</sys:String>
            <sys:String>Png</sys:String>
            <sys:String>Jpg</sys:String>
            <sys:String>Tif</sys:String>
            <sys:String>Gif</sys:String>
        </x:Array>
    </lib:MetroWindow.Resources>


<ComboBox
                    x:Name="CmbItems"
                    HorizontalAlignment="Left"
                    Background="Transparent"
                    ItemsSource="{StaticResource ImageFormatsArray}"
                    SelectedIndex="1"
                    SelectedValue="{Binding SelectedImageFormat}">

                </ComboBox>

1 个答案:

答案 0 :(得分:0)

    <ComboBox
                x:Name="CmbItems"
                Width="100"
                Height="20"
                Background="Transparent"
                ItemsSource="{StaticResource ImageFormatsArray}"
                SelectedValue="{Binding SelectedImageFormat}">
        <ComboBox.Style>
            <Style TargetType="ComboBox">
                <Style.Triggers>
                    <Trigger Property="SelectedValue" Value="{x:Null}">
                        <Setter Property="SelectedIndex" Value="1"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Style>
    </ComboBox>