请告诉我如何将哈希表绑定到WPF组合框。我在WPF Combobox类中找不到DisplayMember,ValueMember属性。
请建议。
此致 约翰。
答案 0 :(得分:3)
这很直截了当。这是一个例子
<强> 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;
}
}