当我尝试使用 DisplayMemberPath 在组合框中显示字典的值时出现错误 [英] DisplayMemberPath gives an error when I try to use it to display the value of my dictionary in a Combo Box

查看:22
本文介绍了当我尝试使用 DisplayMemberPath 在组合框中显示字典的值时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的组合框

<ComboBox Height="40" VerticalAlignment="Stretch" x:Name="comboBox1" Grid.Column="1" FontSize="25">
        </ComboBox>

这是我的 C# 代码

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.Values;
        comboBox1.DisplayMemberPath = "Value";

当我运行我的代码时,我在组合框中看不到任何东西.我做错了什么?

When I run my code I don't see anything in the Combo Box. What have I done wrong?

推荐答案

你应该试试 '.'而不是 Value,它应该选择它作为绑定到对象,因为它没有属性名称

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

comboBox1.DisplayMemberPath = ".";

这篇关于当我尝试使用 DisplayMemberPath 在组合框中显示字典的值时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆