版权声明:本文为博主原创文章,不需博主允许即可随意转载。 https://blog.csdn.net/a_dev/article/details/83302337
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;