c# - How to set ListViewItem's default image if image key could not be found -
is there built in way use default image when listviewitem's imagekey not found?
i have imagelist:
- car.png
- house.png
- default.png
and listviewitem
var item = new listviewitem("item1"); item.imagekey = "computer.png"; by default listview not display image in case. there possibility display "default.png"
this should (though there may more elegant way). need adapt suit how populating listview. basic code this:
string imagekey = "defaultkey"; string someotherkey = "someotherkey"; if (lv.smallimagelist.images.containskey("someotherkey")) { imagekey = someotherkey; } var item = new listviewitem("item1"); item.imagekey = imagekey; note that, depending on how populating list, might better pulled out function returns appropriate key:
string getimagekey(string candidatekey) { if (lvazuredirectory.smallimagelist.images.containskey(candidatekey)) { return candidatekey; } else { return "defaultkey"; } }
Comments
Post a Comment