C#Winform;可能?具有2个数据源的ComboBox,由彩色文本区分

时间:2009-10-22 21:12:50

标签: c# winforms combobox

c#c​​ombobox可以通过排序(顶部的Source 1条目)和Text颜色(例如,蓝色的Source 1条目)区分两个数据源吗?

3 个答案:

答案 0 :(得分:2)

我要做的是将它们组合为一个数据源,根据需要对其进行排序,添加一个指示颜色的属性,然后将文本绑定到一个属性,将颜色绑定到另一个属性。

答案 1 :(得分:2)

您可以从不同的数据源进行查询,并将其转储到单个集合类型中并绑定到组合框。

以下是示例代码,您可以执行与此类似的操作     使用系统;     使用System.Collections.Generic;     使用System.Data;     使用System.Drawing;     使用System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<Item> items; 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {   
            //Let's say we are getting items from two different datasources and putting them in a same collection 
            items = new List<Item>();
            //Getting pens from dataSource1
            items.Add (new Pen("Parker",Color.Blue ));
            items.Add(new Pen("Paper Mate", Color.Blue));
            //Adding Books from dataSource2
            items.Add(new Book("Programming in C", Color.Red));
            items.Add(new Book("Design Patterns", Color.Red));
            //Data binding 
            comboBox1.DataSource = items;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Color";
            this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed; //Do not forget this
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            int index = e.Index;
            Item   item = comboBox1.Items[index] as Item  ;
            if (item.Color.Equals ( Color.Red)) 
                e.Graphics.DrawString(item.Name, this.Font , Brushes.Red, new Point(e.Bounds.X, e.Bounds.Y));
            else if  (item.Color.Equals ( Color.Blue))
                e.Graphics.DrawString(item.Name, this.Font, Brushes.Blue, new Point(e.Bounds.X, e.Bounds.Y));
            else
                e.Graphics.DrawString(item.Name, this.Font, Brushes.Black, new Point(e.Bounds.X, e.Bounds.Y));
        }
    }

    public abstract class  Item
    {
        string name;
        Color  color;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

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

    public class Book:Item  
    {
        public Book(string name, Color color){
            this.Name  = name;
            this.Color = color; 
        }
    }

    public class Pen : Item  
    {
        public Pen(string name, Color color){
            this.Name  = name;
            this.Color  = color; 
        }
    }
}

答案 2 :(得分:0)

我会通过执行两个单独的查询并插入具有指定属性的数组(例如文本颜色,id等),然后将组合框“绑定”到数组来实现此目的。

很抱歉,没有代码示例,因为我对c#并不擅长,但这就是我用我选择的语言实现目标的方法。

相关问题