在使用uint和自定义类时,使用字典作为数据源填充组合框

时间:2015-11-01 07:51:49

标签: c# class dictionary combobox

也许任何人都知道如何让它发挥作用:

我的程序是一个dll,被其他程序使用并从那里获取信息。

字典声明如下:

double **b = &a[1];  // all right, b points to part of a[]
b[0] = a[1];         // this assignment does nothing, they're already equal
b[1] = a[2];         // this assignment does nothing, they're already equal

并将类技能声明为以下内容:

public Dictionary<uint, Skill> Skills { get; }

我的目标是让我的组合框用技能名称填充它,并使用ID作为我从'SelectedItem'获得的值

希望有人可以帮助我解决这个问题,我相信我尝试了50多种不同的方法,我在网上和这里找到...

问候

2 个答案:

答案 0 :(得分:0)

为什么你有uint作为字典键,据我所知,在课堂上同样的uint?

您可以尝试将“ItemsSource”属性绑定到Dictionary(或Dictionary.Values),然后将“DisplayMemberPath”属性绑定到“Name”,并使用“SelectedValuePath”将ID设置为值。

SelectedValuePath
DisplayMemberPath

答案 1 :(得分:0)

public class Item
    {
        public string Name;
        public uint Value;

        public Item(string name, uint value)
        {
            Name = name; Value = value;
        }
        public override string ToString()
        {
            // Generates the text shown in the combo box
            return Name;
        }
    }

 public Dictionary<uint, Skill> Skills = new Dictionary<uint, Skill>();
 private void FormTesting_Load(object sender, EventArgs e)
        {


            Skill tempSkill= new Skill();
            tempSkill.Name = "test name";
            Skills.Add(1, tempSkill);

            foreach (uint val in Skills.Keys)
            {
                comboBox1.Items.Add(new Item(Skills[val].Name,val));
            }
        }

希望它会对你有所帮助。