我在for循环中有以下代码:
ComboboxItem itemb = new ComboboxItem();
itemb.Text = item.gameName;
itemb.Value = item.name;
ASCII_FriendList.Items.Add(itemb);
ASCII_FriendList是列表框,我想显示item.gameName,因为item.name是随机uuid,因此不可读。gameName可以很容易地被识别。
到目前为止,我还没有找到解决方案,我还有什么选择?
IIRC,您可以执行以下操作:
ASCII_FriendList.Items.Add(item);
其中item
是任何对象(例如你的对象包含name
和gameName
),其中我将把作为类型T
。
然后Format
像这样钩住事件:
public Main()
{
InitializeComponent();
ASCII_FriendList.Format += ASCII_FriendList_Format; //You can press tab at += in VS to auto-create the method body.
}
private void ASCII_FriendList_Format(object sender, ListControlConvertEventArgs e)
{
//e.Value will be the string that will display in the list on the form.
if(!(e.ListItem is T))
e.Value = "ListItem isn't T"; //e.ListItem wasn't T, so we can't access gameName
else
e.Value = ((T)e.ListItem).gameName; //Cast e.ListItem to T, then access its gameName property
}
这将ASCII_FriendList.SelectedItem
是类型的对象T
(假设您通过进行了(T)ASCII_FriendList.SelectedItem
强制转换),同时T.gameName
在窗体中显示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句