【发布时间】:2011-01-06 12:02:56
【问题描述】:
请告诉我如何将哈希表绑定到 WPF 组合框。我在 WPF Combobox 类中找不到 DisplayMember、ValueMember 属性。
请指教。
问候, 约翰。
【问题讨论】:
请告诉我如何将哈希表绑定到 WPF 组合框。我在 WPF Combobox 类中找不到 DisplayMember、ValueMember 属性。
请指教。
问候, 约翰。
【问题讨论】:
这很简单。这是一个例子
MainWindow.xaml
<Window ...>
<StackPanel>
<ComboBox ItemsSource="{Binding MyHashTable}"
SelectedValuePath="Key"
DisplayMemberPath="Value"/>
</StackPanel>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public Dictionary<string, string> MyHashTable
{
get;
set;
}
public MainWindow()
{
InitializeComponent();
MyHashTable = new Dictionary<string, string>();
MyHashTable.Add("Key 1", "Value 1");
MyHashTable.Add("Key 2", "Value 2");
MyHashTable.Add("Key 3", "Value 3");
MyHashTable.Add("Key 4", "Value 4");
this.DataContext = this;
}
}
【讨论】: