I have a WPF app that dynamically adds and removes tabs with list views as the content. I want to change the background color of a row in the list view based on the content of one of the columns in the row.

All of the solutions I have seen require using styles in XAML, which is incompatible with my current setup since the list views are created entirely in C# at runtime.

I have tried creating styles and adding data triggers and setters, but it doesn't seem to change anything. There are also multiple list views that should have different styles, so I'm not sure that changing the default style of all list views would be appropriate.

enter image description here

The content of the list view looks like this, and the WAR messages should have a yellow background:

enter image description here

1

There are 1 answers

0
Aaron On

Per the comment from @lidqy, the solution to this was to change

logListView.Style = style;

to

logListView.ItemContainerStyle = style;

The Style declaration line also needed to be changed from

Style style = new Style(typeof(System.Windows.Controls.ListView));

to

Style style = new Style(typeof(System.Windows.Controls.ListViewItem));

since ItemContainerStyle is apparently a style applied to ListViewItem (not ListView).