ImageComboBoxEdit可以实现一个下拉列表,与传统的ComboBox不一样的是,它的每一项都可以加入一个Image。ImageComboBoxEdit的每一项都带有一个ImageIndex(用以指定该项的Image)和一个描述该项的Description字符串,如果字符串为空,就只显示Image,如果ImageIndex为-1,那就只显示Description。在某些场合,我们需要ImageComboBoxEdit可以只显示图像、只显示描述字符串或者两者均显示。仔细研究了ImageComboBoxEdit的属性,并没有找到相应的属性来实现这个功能。虽然使用其Properties下的DisplayFormat可以实现显示内容的格式化,比如{0:F3}之类的可以实现固定小数位数显示浮点数,但对于如何我们提出的这个需求,就没有办法了。当务之急,就只有是自己动手改造一下了。
前面已经讲了ImageComboBoxEdit的特性,那就是当各项的Description为null时它只显示Image,如果ImageIndex为-1,它就只显示Description,我们可以根据这个性质,来实现我们需要功能,思路就是使用两个列表来缓存各项的Description和ImageIndex,然后通过把每一项的Description赋为null来实现只显示Image的功能,或者把每一项的InmageIndex赋为-1来实现只显示描述信息的功能。废话不多说,下面直接上代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
namespace XXXXX
{
public enum ImageComboBoxItemDispalyStyle
{
ShowImageAndDescription,
ShowImageOnly,
ShowDescriptionOnly
}
public partial class CtrImageComboBoxEdit : DevExpress.XtraEditors.ImageComboBoxEdit
{
private List<string> m_Descriptions;
private List<int> m_ImageIndex;
ImageComboBoxItemDispalyStyle m_ItemDisPlayStyle;
public CtrImageComboBoxEdit()
{
InitializeComponent();
m_Descriptions = new List<string>();
m_ImageIndex = new List<int>();
m_ItemDisPlayStyle = ImageComboBoxItemDispalyStyle.ShowImageAndDescription;
}
public void AddItem(string description, object value, int imageIndex)
{
m_Descriptions.Add(description);
m_ImageIndex.Add(imageIndex);
if (m_ItemDisPlayStyle == ImageComboBoxItemDispalyStyle.ShowImageAndDescription)
this.Properties.Items.Add(new ImageComboBoxItem(description, value, imageIndex));
else if (m_ItemDisPlayStyle == ImageComboBoxItemDispalyStyle.ShowDescriptionOnly)
this.Properties.Items.Add(new ImageComboBoxItem(description, value, -1));
else if (m_ItemDisPlayStyle == ImageComboBoxItemDispalyStyle.ShowImageOnly)
this.Properties.Items.Add(new ImageComboBoxItem(null, value, imageIndex));
}
public ImageComboBoxItemDispalyStyle ItemDisPlayStyle
{
get { return m_ItemDisPlayStyle; }
set
{
m_ItemDisPlayStyle = value;
if (this.Properties.Items.Count > 0)
{
if (m_ItemDisPlayStyle == ImageComboBoxItemDispalyStyle.ShowImageAndDescription)
{
for (int i = 0; i < this.Properties.Items.Count; i++)
{
this.Properties.Items[i].Description = m_Descriptions[i];
this.Properties.Items[i].ImageIndex = m_ImageIndex[i];
}
}
else if(m_ItemDisPlayStyle == ImageComboBoxItemDispalyStyle.ShowDescriptionOnly)
{
for (int i = 0; i < this.Properties.Items.Count; i++)
{
this.Properties.Items[i].Description = m_Descriptions[i];
this.Properties.Items[i].ImageIndex = -1;
}
}
else if (m_ItemDisPlayStyle == ImageComboBoxItemDispalyStyle.ShowImageOnly)
{
for (int i = 0; i < this.Properties.Items.Count; i++)
{
this.Properties.Items[i].Description = null;
this.Properties.Items[i].ImageIndex = m_ImageIndex[i];
}
}
}
}
}
public new string SelectedText
{
get
{
if (this.SelectedIndex != -1) return m_Descriptions[this.SelectedIndex];
else return null;
}
}
}
}
对于这个自己定制后的ImageComboBoxEdit,可以在设计阶段或运行中通过更改ItemDisPlayStyle属性来实现显示状态的切换。唯一需要注意的就是添加项时直接使用AddItem添加,当然如果直接通过访问Properties的Items来添加就无法实现本文提到的这个功能了。