如何创建ColorComboBox并将其ValueMember绑定到我的类对象的Color属性? [英] How to create ColorComboBox and bind its ValueMember to Color property of my class object?

查看:201
本文介绍了如何创建ColorComboBox并将其ValueMember绑定到我的类对象的Color属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建Windows窗体应用程序,应该允许用户通过ComboBox选择对象的颜色属性。 MyClass类型的对象存储在单独的列表中,该列表绑定到一个ComboBox。此ComboBox显示对象ID。

I am creating Windows Form Application, that should allow user to select color property of objects via ComboBox. Objects of type MyClass are stored in separate list, which is binded to one ComboBox. This ComboBox shows object ID.

PictureBox也会显示目前所选物件的颜色。这些绑定是做得很好,但我很难绑定颜色属性的MyClass对象的ComboBox的ValueMember属性。

PictureBox shows color of currently selected object via binding as well. These bindings are doing well, but I struggle to bind color property of MyClass object to ValueMember property of ComboBox.

这个ComboBox有所有System.Drawing.Color颜色的列表(现在作为文本项)。我需要绑定当前选择到MyClass.cr属性。如何做呢?

This ComboBox has the list of all System.Drawing.Color colors (as text items for now). I need to bind current selection to MyClass.cr property. How to do that?

我也想在ComboBox中绘制用对应颜色而不是文本项填充的矩形。我应该使用所有者绘制组合框,还是更简单的方法?

I also want to draw rectangles filled with correspondent color instead of text items in ComboBox. Should I use owner draw combobox, or is there simpler way?

这里是代码:

  public partial class Form1 : Form
  {
    List<MyClass> MyClassList = new List<MyClass>();
    public Form1()
    {
        InitializeComponent();
        // Create the list of objects typof(MyClass)
        for (int i = 0; i < 10; i++)
            MyClassList.Add(new MyClass(i));

        // Set BindingSource
        MyClassBindingSource.DataSource = MyClassList;
        // Add bindings to ComboBox with IDs
        SelectIDComboBox.DataSource = MyClassBindingSource;
        SelectIDComboBox.DisplayMember = "ID";

        // Show color of current ID in special PictureBox
        ShowColorPictureBox.DataBindings.Add("BackColor", MyClassBindingSource, "cr", true, DataSourceUpdateMode.OnPropertyChanged);

        // Allow user to change color, selecting it from another ComboBox
        string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));

        // ComboBox must show rectangles filled with corresponding color, not color names
        SelectColorComboBox.Items.AddRange(colors);// for now only color names
        // Bind selected color to MyClass.cr property
        //????
    }
}
public class MyClass
{
    public MyClass(int id) { ID = id.ToString(); }
    public Color cr { get; set; } = Color.Red;
    public string ID { get; set; }
}


推荐答案

>

Binding:

SelectColorComboBox.DataBindings.Add("Text", MyClassBindingSource, "cr", true, DataSourceUpdateMode.OnPropertyChanged);

绘制用对应颜色填充的矩形。

设置 DrawMode 属性并订阅 DrawItem 事件:

Draw rectangles filled with correspondent color.
Set DrawMode property and subscribe to DrawItem event:

SelectColorComboBox.DrawMode = DrawMode.OwnerDrawFixed;
SelectColorComboBox.DrawItem += SelectColorComboBox_DrawItem;

private void SelectColorComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
    var comboBox = (ComboBox)sender;
    var strColor = (string)comboBox.Items[e.Index];
    var knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), strColor);
    var color = Color.FromKnownColor(knownColor);

    e.DrawBackground();
    e.DrawFocusRectangle();

    using (var brush = new SolidBrush(color))
        e.Graphics.FillRectangle(brush, e.Bounds.X, e.Bounds.Y + 1, 30, e.Bounds.Height - 2);

    TextRenderer.DrawText(e.Graphics, strColor, comboBox.Font, new Point(e.Bounds.X + 30, e.Bounds.Y), color);
}

这篇关于如何创建ColorComboBox并将其ValueMember绑定到我的类对象的Color属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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