c# - Change ListBox Item Background color programmatically -
i having real hard time understanding how xaml works in connection c#. problem is, have 2 different list<string>
objects filled content, want 1 list<string>
have backgroundcolor 'blue' , other 1 have background color 'red'. afterwards want display in listbox
my xaml listbox code:
<listbox x:name="listbox1" horizontalalignment="left" height="240" margin="81,80,0,0" verticalalignment="top" width="321" borderbrush="#ff6c6c6c" selectionmode="single" selectionchanged="listboxselectionchanged"> </listbox>
my c# code loads content listbox
public partial class mainwindow : window { public void additemstolistbox() { foreach (var object1 in objects1) { thread.sleep(1); listbox1.items.add(object1.label); //i want these objects blue } foreach (var object2 in objects2) { thread.sleep(1); listbox2.items.add(object2.label); //i want these objects red } } }
here go:
foreach (var object1 in objects1) { thread.sleep(1); listbox1.items.add(new listboxitem { content = object1.label, background = brushes.blue }); } foreach (var object2 in objects2) { thread.sleep(1); listbox2.items.add(new listboxitem { content = objects2.label, background = brushes.red }); //i want these objects red }
a better way use data binding, styles, etc.
Comments
Post a Comment