将Hashtable绑定到WPF组合框

时间:2011-01-06 12:02:56

标签: c# wpf combobox hashtable

请告诉我如何将哈希表绑定到WPF组合框。我在WPF Combobox类中找不到DisplayMember,ValueMember属性。

请建议。

此致 约翰。

1 个答案:

答案 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;
    }
}