【发布时间】:2020-04-10 05:40:52
【问题描述】:
我有一个组合框,我已将它绑定到字典。
字典定义为:
Cities = new Dictionary<string, string>
{
{"USA", "NewYork"},
{"UK", "London"},
{"Canada", "Toronto"}
};
组合框与字典的绑定如下:
<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>
绑定工作正常,但我无法将组合框的选定项设置为“伦敦”。
由于 itemtemplate 不是单个字符串属性,因此组合框 selecteditem 属性未绑定到字符串值“London”
谢谢, 萨特
【问题讨论】:
标签: wpf xaml combobox selecteditem itemtemplate