【问题标题】:Binding Hashtable to a WPF Combobox将哈希表绑定到 WPF 组合框
【发布时间】:2011-01-06 12:02:56
【问题描述】:

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

请指教。

问候, 约翰。

【问题讨论】:

    标签: c# wpf combobox hashtable


    【解决方案1】:

    这很简单。这是一个例子

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

    【讨论】:

    • 谢谢Meleak,我会试试的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多