如何将 ListBox 的值设置为字符串
How to set value of ListBox as a string
我在 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,因此不可读。游戏名称很容易识别。
到目前为止我还没有找到解决方案,我有什么选择?
IIRC 您可以执行以下操作:
ASCII_FriendList.Items.Add(item);
其中 item
是 any 对象(例如包含 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
。
我在 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,因此不可读。游戏名称很容易识别。
到目前为止我还没有找到解决方案,我有什么选择?
IIRC 您可以执行以下操作:
ASCII_FriendList.Items.Add(item);
其中 item
是 any 对象(例如包含 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
。