Sunday, 15 March 2015

c# - Update a listboxitem through code where a specific column = value x -



c# - Update a listboxitem through code where a specific column = value x -

i have wpf form listbox has 2 columns.

i utilize code add together values listbox :

playersonlinelist.items.add(new { username = username, status = "lobby" });

i utilize code remove values listbox:

playersonlinelist.items.remove(new { username = username, status = "lobby" });

my question how update value of "status" username = x?

thanks

the problem using anonymous type. items returned items collection of listbox returned object. in order access properties of items need cast them, cannot since type anonymous. create explicit class items.

class userstatus { public string username { get; set; } public string status { get; set; } public override bool equals(object obj) { userstatus other = obj userstatus; homecoming other != null && other.username == username && other.status == status; } public override int gethashcode() { unchecked { int hash = 17; hash = hash * 31 + username == null ? 0 : username.gethashcode(); hash = hash * 31 + status == null ? 0 : status.gethashcode(); homecoming hash; } } }

now can search index of item

for (int = 0; < playersonlinelist.items.count; i++) { var userstatus = (userstatus)playersonlinelist.items[i]; if (userstatus.username == x) { userstatus.status = newstatus; break; } }

c# listbox refresh listboxitem

No comments:

Post a Comment