WPF: How do I programmatically style my ListView so that all rows have a certain background color and height? -
i can't seem wrap head around something. have following logic in code:
namespace wpftesting { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { observablecollection<message> messages = new observablecollection<message>(); public mainwindow() { initializecomponent(); messages.add(new message(datetime.now, "this test.")); listview listview = new listview(); gridview gridview = new gridview(); listview.view = gridview; gridviewcolumn timestampcolumn = new gridviewcolumn(); timestampcolumn.displaymemberbinding = new binding("date"); gridviewcolumnheader timestampheader = new gridviewcolumnheader(); timestampheader.content = "time"; timestampcolumn.header = timestampheader; gridview.columns.add(timestampcolumn); gridviewcolumn messagecolumn = new gridviewcolumn(); messagecolumn.displaymemberbinding = new binding("text"); gridviewcolumnheader messageheader = new gridviewcolumnheader(); messageheader.content = "message"; messagecolumn.header = messageheader; gridview.columns.add(messagecolumn); binding binding = new binding(); binding.source = messages; listview.setbinding(itemscontrol.itemssourceproperty, binding); maingrid.children.add(listview); } public class message { public message(datetime adate, string atext) { date = adate; text = atext; } public datetime date { get; set; } public string text { get; set; } } } }
how programmatically style listview rows have background color , height? note, want avoid doing foreach loop on listview's items list , setting each listviewitem's properties, because list may have many items , expensive. instead, there not way programmatically using style class, or perhaps run-time logic?
i figured out. can way example:
style style = new style(); style.targettype = typeof(listviewitem); style.setters.add(new setter(listviewitem.backgroundproperty, brushes.pink)); listview.itemcontainerstyle = style;
edit: can conditionally set style on listview's items using trigger values data set. found useful, may others well:
datatrigger trigger = new datatrigger(); trigger.binding = new binding("text"); trigger.value = "this test."; trigger.setters.add(new setter(listviewitem.backgroundproperty, brushes.pink)); style.triggers.add(trigger); listview.itemcontainerstyle = style;
the above code set background of row under condition text field set "this test."
Comments
Post a Comment