1

在以下代码中:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BindingSourceSimpleTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class Color
        {
            public string Name { get; set; }
            public int Red { get; set; }
            public int Green { get; set; }
            public int Blue { get; set; }
        }

        public class Obj
        {
            public string Name { get; set; }
            public string Color { get; set; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var colors = new List<Color>();
            colors.Add(new Color { Name = "Black", Red = 0, Green = 0, Blue = 0 });
            colors.Add(new Color { Name = "Blue", Red = 0, Green = 0, Blue = 255 });
            colors.Add(new Color { Name = "Red", Red = 255, Green = 0, Blue = 0 });
            colors.Add(new Color { Name = "Magenta", Red = 255, Green = 0, Blue = 255 });
            colors.Add(new Color { Name = "Green", Red = 0, Green = 255, Blue = 0 });
            colors.Add(new Color { Name = "Cyan", Red = 0, Green = 255, Blue = 255 });
            colors.Add(new Color { Name = "Yellow", Red = 255, Green = 255, Blue = 0 });
            colors.Add(new Color { Name = "White", Red = 255, Green = 255, Blue = 255 });

            var objs = new List<Obj>();
            objs.Add(new Obj { Name = "Sun", Color = "Yellow" });
            objs.Add(new Obj { Name = "Grass", Color = "Green" });
            objs.Add(new Obj { Name = "Blood", Color = "Red" });
            objs.Add(new Obj { Name = "Sky", Color = "Blue" });
            objs.Add(new Obj { Name = "Hair", Color = "Black" });
            objs.Add(new Obj { Name = "Snow", Color = "White" });
            objs.Add(new Obj { Name = "Rose", Color = "Red" });

            listBoxObjs.DataSource = new BindingSource(objs, null);
            listBoxColors.DataSource = new BindingSource(colors, null);
            comboBoxObjColor.DataSource = new BindingSource(colors, null);

            textBoxObjName.DataBindings.Add("Text", listBoxObjs.DataSource, "Name");
            comboBoxObjColor.DataBindings.Add("SelectedItem", listBoxObjs.DataSource, "Color");
            textBoxColorName.DataBindings.Add("Text", listBoxColors.DataSource, "Name");
            textBoxColorRed.DataBindings.Add("Text", listBoxColors.DataSource, "Red");
            textBoxColorGreen.DataBindings.Add("Text", listBoxColors.DataSource, "Green");
            textBoxColorBlue.DataBindings.Add("Text", listBoxColors.DataSource, "Blue");

            listBoxObjs.DisplayMember = listBoxColors.DisplayMember = comboBoxObjColor.DisplayMember = "Name";
        }
    }
}

和以下表单 UI:

表格1
(来源: persiangig.com )

  1. 我看到太阳是黑色的!为什么?!经过深入研究后,我发现颜色列表中的项目是 Color 类型,而对象的 Color 属性是 string 类型。
  2. 如何使对象的 Color 属性在组合框中正确显示?
  3. 如果我解决了这个问题,更改组合框选择是否也会正确更新对象的这个属性?
  4. 如果我解决这个问题,太阳会变黄吗?:)

谢谢

4

1 回答 1

0

看起来您在 Obj 列表中缺少对颜色对象的引用。它有一个 Color 属性,它是字符串,但与 Color 列表中的 Color 对象没有关系。

    public class Obj
    {
        public string Name { get; set; }
        public BindingSourceSimpleTest.Color Color { get; set; }
    }

此外,您的类应该实现 INotifyPropertyChanged 接口,以便数据绑定正常工作:

public class Obj : INotifyPropertyChanged {
  public event PropertyChangedEventHandler PropertyChanged;

  private string name;
  private BindingSourceSimpleTest.Color color;

  protected void OnPropertyChanged(string propName) {
    if (PropertyChanged != null) {
      PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
  }

  public string Name {
    get { return name; }
    set {
      name = value;
      OnPropertyChanged("Name");
    }
  }

  public BindingSourceSimpleTest.Color Color {
    get { return color; }
    set {
      color = value;
      OnPropertyChanged("Color");
    }
  }
}
于 2012-08-10T14:21:58.980 回答