更新ListBox的BackColor

时间:2016-10-10 08:58:28

标签: c# winforms binding

我需要通过从组合框中选择颜色并单击按钮来更新我的ListBox的BackColor。这是我的代码

      public Data _current;
    public Data Current
    {
        get
        { return _current; }
        set
        {
            _current = value;

            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Current"));
            }
        }
    }
    public Form1()
    {
        InitializeComponent();
        Items = new ObservableCollection<Data>();
        for (int i = 0; i < names.Count; i++)
        {
            Items.Add(new Data() {Name=names[i].ToString().TrimStart(new char[] { '[', 'C', 'o', 'l', 'o', 'r', ' ', ']' }).TrimEnd(new char[] { ']' }), color = names[i] });
        }

        comboBox1.DataSource = Items;
        comboBox1.DisplayMember ="Name";
        Current = Items.First();

    }
    public List<Color> names = new List<Color>() { Color.Red,Color.Yellow,Color.Green,Color.Blue };

    public event PropertyChangedEventHandler PropertyChanged;
    private void button1_Click(object sender, EventArgs e)
    {
        Current = comboBox1.SelectedItem as Data;
        listBox1.DataBindings.Add("BackColor", Current, "color", true, DataSourceUpdateMode.OnPropertyChanged);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
}
public class Data:INotifyPropertyChanged
{
    private Color _color;

    public string name { get; set; }
    public string Name
    {
        get
        { return name; }
        set
        {
            name = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }
    public Color color
    {
        get
        {
            return _color;
        }
        set
        {
            _color = value;
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("color"));
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

异常弹出并说:&#34;这导致集合中的两个绑定绑定到同一属性。&#34;

2 个答案:

答案 0 :(得分:2)

为解决原始问题&#34;我需要通过从组合框中选择颜色并在单击按钮时更新我的​​ListBox的BackColor。你根本不需要数据绑定。你可以用

来做
private void button1_Click( object sender, EventArgs e )
{
    listBox1.BackColor = (Color)comboBox1.SelectedValue;
}

如果你真的想用数据绑定来做,你可以直接在listbox和combobox之间绑定(好吧,这不涉及按钮点击):

listBox1.DataBindings.Add( "BackColor", comboBox1, "SelectedValue", true, DataSourceUpdateMode.OnPropertyChanged );

如果你需要数据绑定和buttonclick,你可以使用form.Current

绑定组合框
comboBox1.DataBindings.Add( "SelectedItem", this, "Current", true, DataSourceUpdateMode.OnPropertyChanged );

并点击按钮更新背景颜色:

private void button1_Click( object sender, EventArgs e )
{
    listBox1.BackColor = Current.Color;
}

以下是包含更多改进的完整示例:

public class Data : INotifyPropertyChanged
{
    public string Name
    {
        get { return color.Name; }
    }

    private Color color;
    public Color Color
    {
        get { return color; }
        set
        {
            color = value;

            if( PropertyChanged != null )
                this.PropertyChanged( this, new PropertyChangedEventArgs( "Color" ) );
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

public class Form1 : Form
{
    private ComboBox comboBox1;
    private ListBox listBox1;
    private Button button1;

    public Form1()
    {
        InitializeComponent();

        List<Data> dataList = new List<Data>
        {
            new Data { Color = Color.Red },
            new Data { Color = Color.Yellow },
            new Data { Color = Color.Green },
            new Data { Color = Color.Blue },
        };

        comboBox1.DataSource = dataList;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Color";
    }

    public Data Current
    {
        get { return comboBox1.SelectedItem as Data; }
    }

    private void button1_Click( object sender, EventArgs e )
    {
        listBox1.BackColor = Current.Color;
    }

    private void InitializeComponent()
    {
        //...
    }
}

答案 1 :(得分:1)

您只能将控件的任何属性绑定一次,否则您将收到ArgumentException

  

这会导致集合中的两个绑定绑定到同一个绑定   属性。参数名称:binding

在您的代码中,您在按钮的单击事件处理程序中添加了数据绑定,并且每次单击代码执行时。

要解决此问题,请将数据绑定代码移动到表单的Load事件。

如果出于任何原因想要在按钮的click事件中添加绑定,要解决问题,您可以检查是否尚未添加绑定,然后添加绑定:

if (listBox1.DataBindings["BackColor"] == null)
    listBox1.DataBindings.Add("BackColor", Current, "color", true, 
        DataSourceUpdateMode.OnPropertyChanged);