[英]WPF - Combobox with button in ItemTemplate, click on SelectedItem
[英]SelectedItem not working in a wpf combobox after the itemtemplate has been modified
我有一個組合框,並將它綁定到字典。
字典定義為:
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”
謝謝,薩斯
您可以將SelectedValuePath
屬性設置為“值”。 然后,當您將SelectedValue
設置為“London”時,ComboBox 會將其選擇映射到將“London”設置為其Value
屬性的項目,這將解決您的問題這里有一篇文章,詳細介紹了選擇的不同用法特性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.