如何使Devexpress的ImageComboBoxEdit只显示Image或Description

本文介绍了一种自定义ImageComboBoxEdit的方法,使其能够根据需求仅显示图像、仅显示描述文字或同时显示两者。通过维护两个列表分别存储描述文字和图像索引,实现了不同显示模式的灵活切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    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来添加就无法实现本文提到的这个功能了。

这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括以下内容:文本(Text)、缩进的级别(Level)、图标的索引(ImageIndexImageKey),用户数据(Tag)。ImageComboBoxItem类实现了ISerializable接口,实现自定义序列化。ImageComboBoxItem类的类视图如下: 图3 ImageComboxItem类视图 ImageComboBoxItem类的代码如下: [Serializable] [DefaultProperty("Text")] [TypeConverter( typeof(ExpandableObjectConverter))] public class ImageComboBoxItem : IDisposable, ISerializable ...{ Fields#region Fields private ImageComboBox _imageComboBox; private string _text = "ImageComboBoxItem"; private ImageComboBoxItemImageIndexer _imageIndexer; private object _tag; private int _level; #endregion Constructors#region Constructors public ImageComboBoxItem() ...{ } public ImageComboBoxItem(string text) : this(text, -1, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex) : this(text, imageIndex, 0) ...{ } public ImageComboBoxItem( string text, string imageKey) : this(text, imageKey, 0) ...{ } public ImageComboBoxItem( string text, int imageIndex, int level) : this() ...{ _text = text; ImageIndexer.Index = imageIndex; _level = level; } public ImageComboBoxItem( string text, string imageKey, int level) : this() ...{ _text = text; ImageIndexer.Key = imageKey; _level = level; } protected ImageComboBoxItem( SerializationInfo info, StreamingContext context) : this() ...{ Deserialize(info, context); } #endregion Properties#region Properties [Localizable(true)]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深蓝静音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值