1

我想将我的组合框 ItemsSourse 绑定到

ObservableCollection<KeyValuePair<object, string>>.

我怎样才能做到这一点?

4

2 回答 2

1

您可以将 ItemsSource 绑定到 ObservableCollection,然后将DisplayMemberPath设置为 Value:

<ComboBox ItemsSource="{Binding YourCollection}" DisplayMemberPath="Value" />

然后,组合框中的值将匹配 KeyValuePairs 中的值。

于 2013-11-29T22:41:16.827 回答
1

最简单的方法是使用该DisplayMemberPath属性:

<ComboBox ItemsSource="{Binding Pairs}" DisplayMemberPath="Value" />

或者,您可以在视图模型中公开一个仅包含值的新属性。例如:

public ObservableCollection<string> AllValues { get; set; }

public ViewModel()
{
    AllValues = new ObservableCollection<string>(Pairs.Select(x => x.Value));
}
<ComboBox ItemsSource="{Binding AllValues}" />
于 2013-11-29T22:41:23.167 回答