1

I want to highlight the row when I select from my ListView but I cant get it to work. Can anyone look at what I have and tell me what I'm doing wrong? Another question is What about having a property in my ViewModel and setting the Background color based on the bool value, how can that be achieve?

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Green" />
            </Trigger>

            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
user1884032
  • 337
  • 1
  • 5
  • 18

3 Answers3

3

The problem here is the item template for the ListView automatically adds the "Selected highlight" of brush type SystemColors.HighlightBrushKey - the "true solution" would be to override the item template definition, but one way you can get what you're after here is something like this:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>
JerKimball
  • 16,584
  • 3
  • 43
  • 55
0

You can bind a Bool value to a Color brush by using an interface called IValueConverter.

Here's a related post: What is best practise for IValueConverter?

Community
  • 1
  • 1
Joe
  • 2,496
  • 1
  • 22
  • 30
0
            <ListView  Name="listBox1" ItemsSource="{Binding Path=SimpleList}"
                      HorizontalAlignment="Left" VerticalAlignment="Top" Background="Olive">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <EventSetter Event="MouseEnter" Handler="listBox1_ListBoxItem_MouseEnter"/>
                        <EventSetter Event="MouseLeave" Handler="listBox1_ListBoxItem_MouseLeave"/>
                        <Style.Resources>
                            <!-- Background of selected item when focussed -->
                            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                Color="Green"/>
                            <!-- Background of selected item when not focussed -->
                            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                                Color="Yellow" />
                        </Style.Resources>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <!--<Setter Property="FontWeight" Value="Bold" />-->
                                <Setter Property="Background" Value="Orange" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • 1
    The normal trigger he had for "IsMouseOver" should work fine - it's the selected style which gets overriden by the ItemTemplate. – JerKimball Dec 31 '12 at 21:36