开发者

Binding Hashtable to a WPF Combobox

开发者 https://www.devze.com 2023-02-02 14:46 出处:网络
Please tell me how can i bind a hashtable toa WPF C开发者_StackOverflowombobox. I cannot find the DisplayMember,ValueMember properties in the WPF Combobox class.

Please tell me how can i bind a hashtable to a WPF C开发者_StackOverflowombobox. I cannot find the DisplayMember,ValueMember properties in the WPF Combobox class.

Please advice.

Regards, John.


It's pretty straight forward. Here's an example

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消