【问题标题】:Update BackColor for ListBox更新列表框的背景颜色
【发布时间】:2016-10-10 08:58:27
【问题描述】:

我需要通过从组合框中选择颜色并单击按钮来更新 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;
}

异常弹出并说:“这会导致集合中的两个绑定绑定到同一个属性。”

【问题讨论】:

  • 如果您想使用ClickButton 事件更新ListBox 的背景颜色,则不需要数据绑定来更新ListBox 的背景颜色。

标签: c# winforms binding


【解决方案1】:

为了解决您的原始问题“我需要通过从组合框中选择颜色并单击按钮来更新 ListBox 的 BackColor”,您根本不需要数据绑定。你可以这样做

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

如果你真的想用数据绑定来做,你可以直接在列表框和组合框之间进行绑定(好吧,这不涉及按钮点击):

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

如果您需要数据绑定和按钮单击,您可以将组合框与 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()
    {
        //...
    }
}

【讨论】:

    【解决方案2】:

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

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

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

    为了解决这个问题,将数据绑定代码移动到表单的 Load 事件中。

    如果出于某种原因你想在按钮的点击事件中添加绑定,为了解决这个问题你可以检查是否没有添加绑定然后添加绑定:

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

    【讨论】:

    • 是的,我尝试将 DataBinding 代码移至 Form_Load 事件,但我需要在按钮 Click Event 中写入内容。
    • 没问题,你可以检查if (listBox1.DataBindings["BackColor] == null)然后添加绑定。
    • 我在点击事件中写了上面的代码然后颜色只改变一次实际上实验是从组合框中选择一种颜色,当点击按钮时,列表框的背景颜色会相应地更新..所以在这里它只在颜色不更新后更新一次..
    • 这篇文章回答了你关于你收到的异常的问题。
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2015-05-17
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多