DisplayMemberPath gives an error when I try to use it to display the value of my dictionary in a Combo Box

前端 未结 2 1257
情书的邮戳
情书的邮戳 2021-01-25 04:59

Here is my Combo Box


        

        
相关标签:
2条回答
  • 2021-01-25 05:26

    you should try '.' instead of Value, it should pick it up as binding to the object as it has no property name

    comboBox1.DisplayMemberPath = ".";
    
    0 讨论(0)
  • 2021-01-25 05:29

    Try this:

    <ComboBox Height="40" VerticalAlignment="Stretch" SelectedValuePath="Key" DisplayMemberPath="Value" x:Name="comboBox1" FontSize="25"/>
    
    var source = new Dictionary<string, double>();
                source.Add("Item1", 0.4);
                source.Add("Item2", 0.3);
                source.Add("Item3", 0.1);
                source.Add("Item4", 0.1);
    
                var formateDSource = new Dictionary<string, string>();
    
                foreach (var item in source)
                {
                    formateDSource.Add(string.Format("[{0}, {1}]", item.Key, item.Value), item.Key);
                }
    
                comboBox1.ItemsSource = source;
    
    0 讨论(0)
提交回复
热议问题