我想将我的组合框 ItemsSourse 绑定到
ObservableCollection<KeyValuePair<object, string>>.
我怎样才能做到这一点?
我想将我的组合框 ItemsSourse 绑定到
ObservableCollection<KeyValuePair<object, string>>.
我怎样才能做到这一点?
您可以将 ItemsSource 绑定到 ObservableCollection,然后将DisplayMemberPath设置为 Value:
<ComboBox ItemsSource="{Binding YourCollection}" DisplayMemberPath="Value" />
然后,组合框中的值将匹配 KeyValuePairs 中的值。
最简单的方法是使用该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}" />