Friday, August 14, 2009

How to set WPF ListView selected item background color

After my last post I came across another common WPF task that could result in excruciating frustration if you're not using Expression Blend (or you just don't know enough about this shit nice framework): setting the color of the selected item in a ListView.

This is how you do it:

<ListView>
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Margin" Value="0"/>
<!-- here we go -->
<Style.Resourcesgt>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Gray"/>
</Style.Resources>
</Style>
</ListView.Style>
<!-- other stuff -->
<ListView>

Hope it helps - if so you can express your gratitude by randomly upvoting some of my answers on stackoverflow.

4 comments:

Anonymous said...

Thanks, this really helped :D

der_Tankwart said...

Hello
Cool Blog!

but:Perhaps, there is a failure in your code:

is the "gt" not false?

Barrakoda said...

If you combine this with http://dotnetbutchering.blogspot.com/2009/08/how-to-set-wpf-listview-alternate-row.html, the result does not work on Windows 7/Aero.

Barrakoda said...

If you combine this example with http://dotnetbutchering.blogspot.com/2009/08/how-to-set-wpf-listview-alternate-row.html the result does not work with Windows 7/Aero.