C#为ComboBox等数组型控件设置自定义数据

ComboBox、ListBox、CheckedListBox等列表型控件,可以单独为每个Item设置显示文本和数据。

为此,我们定义一个类,来实现这个Item的文本显示和数据关联:

    public class ListComponentItem
    {
        private string _text = null;
        private object _value = null;

        public string Text { get => _text; set => _text = value; }
        public object Value { get => _value; set => _value = value; }

        public ListComponentItem(string text, object value = null)
        {
            Text = text;
            Value = value;
        }
        public override string ToString()
        {
            return _text;
        }
    }

在使用时,只需初始化控件的Items即可:

foreach (var pFeatureLayer in lstPolygonFeatureLayer)
{
    comboBoxPolygonLayer.Items.Add(new ListComponentItem(pFeatureLayer.Name, pFeatureLayer.FeatureClass));
}

或:

lstPolygonFeatureLayer.ForEach(e => comboBoxPolygonLayer.Items.Add(new ListComponentItem(e.Name, e.FeatureClass)));

那么,当我们选中某个Item时,通过如下方式取得其中的数据:

var pFeatureClass = (IFeatureClass)((ListComponentItem)comboBoxPolygonLayer.SelectedItem).Value;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值