修改itemtemplate后,选择EdItem不在wpf组合框中工作

修改itemtemplate后,选择EdItem不在wpf组合框中工作,wpf,xaml,combobox,selecteditem,itemtemplate,Wpf,Xaml,Combobox,Selecteditem,Itemtemplate,我有一个组合框,我把它装订在字典上了 词典的定义如下: Cities = new Dictionary<string, string> { {"USA", "NewYork"}, {"UK", "London"}, {"Canada", "Toronto"} }; Cities=新词典 { {“美国”、“纽约”}, {“英国”、“伦敦”}, {“加拿大”、“多伦多

我有一个组合框,我把它装订在字典上了

词典的定义如下:

        Cities = new Dictionary<string, string>
        {
            {"USA", "NewYork"},
            {"UK", "London"},
            {"Canada", "Toronto"}
        };
Cities=新词典
{
{“美国”、“纽约”},
{“英国”、“伦敦”},
{“加拿大”、“多伦多”}
};
组合框绑定到字典,如下所示:

                    <ComboBox x:Name="CitiesComboBox" Grid.Column="1" ItemsSource="{Binding Cities}" SelectedItem="{Binding SelectedCity}" Margin="10" Background="Transparent">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Key}" FontSize="15" VerticalAlignment="Center"/>
                                    <TextBlock Text="{Binding Value, Mode=OneWay, StringFormat='{}  {0}'}" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

装订工作正常,但我无法将组合框的selecteditem设置为“London”

由于itemtemplate不是单个字符串属性,因此combobox selecteditem属性未绑定到字符串值“London”

谢谢,
Sath

您可以将
SelectedValuePath
属性设置为“Value”。然后,当您将
SelectedValue
设置为“London”时,ComboBox将其选择映射到将“London”设置为其
Value
属性的项目,这将解决您的问题 是一篇关于选择属性的不同用法的更详细的文章