1

How do I do this? Everything I've founds says this should work, but it doesn't for me. What am I doing wrong?

<ListView ItemsSource="{Binding ListViewItems}" >
    <ListView.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
            </Style.Resources>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Chris
  • 1,005
  • 1
  • 13
  • 27

2 Answers2

0

Try to directly set it in <ListView.Resources><SolidColorBrush x:Key=...></ListView.Resources>

Try to define a SolidColorBrush with Key "{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" too.

UPDATE: Just tried to run the following xaml on win7 and targeting .net 4.5:

    <ListView ItemsSource="{Binding DummyItems}">
        <ListView.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="LightGreen"/>
        </ListView.Resources>
    </ListView>

Works like a charm! Nevertheless, it seems to be that this is not going to work on win8. So there may be no way of getting around to define your own template. Refer to this page.

Christian
  • 143
  • 8
0

The code you have shown above does work for me. This, however, sets the colors when ListBox is in focus. To set the color when it is not in focus, try ...

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />

Check this example to implement it on Win 8.

Community
  • 1
  • 1
Nishant
  • 443
  • 3
  • 9
  • I don't know if I have some bug with my Visual Studio or what but the code I posted doesn't do anything at all. I added your code and that didn't do anything either. I'm getting really frustrated because all the code I've found on the web does absolutely nothing to my ListView, as if no code is there at all. – Chris Jun 05 '13 at 22:37
  • It may be due to Windows theme. What is your Windows OS version and theme? I have noticed some color related issues on Win 8. – Nishant Jun 07 '13 at 15:56
  • 1
    [Check this](http://stackoverflow.com/questions/12007918/list-combo-box-background-and-selected-colours-under-net-4-5)... It appears that you'll need to write your own template for ListBoxItem if you are running your app on Win 8. – Nishant Jun 07 '13 at 16:06
  • That link solved it! Thanks for finding it. Now I'm not sure what how to accept this. Maybe edit your post to include the info from the link? – Chris Jun 10 '13 at 20:53