首页 > 解决方案 > 将值从字典绑定到 ComboBox

问题描述

我有一个清单:

public SortedDictionary<string, string> ProjectDictionaryList { get; internal set; }

现在我想将值绑定到 ComboBox:

ComboBox DockPanel.Dock="Right" ItemsSource="{Binding Path=ProjectDictionaryList}" 
         IsSynchronizedWithCurrentItem="True" Style="{StaticResource myCombo}" 
         SelectedItem="{Binding SelectedProject}"

但是怎么绑定ProjectDictionaryList.Value

标签: c#wpfdictionarydata-bindingcombobox

解决方案


设置DisplayMemberPathSelectedValuePath绑定SelectedValue以通过其键选择字典条目:

<ComboBox
    ItemsSource="{Binding ProjectDictionaryList}" 
    DisplayMemberPath="Value"
    SelectedValuePath="Key"
    SelectedValue="{Binding SelectedProject}"/>

推荐阅读