HaloMediaz
HaloMediaz

Reputation: 1263

ListView Item Control Template triggers not firing

I have a ListView that contains a GridView. I am trying to change both the background of an item that is selected, and the background of an item that is hovered over. After some searching, I came across the control template in conjunction with triggers, but they're not firing.

Here is a screenshot of what I mean enter image description here

<ListView x:Name="TestListView" Margin="0,0.333,0.333,0" Grid.Row="1" Grid.Column="1" SelectedIndex="0" MouseDoubleClick="Song_List_DoubleClick" SelectionMode="Single" Background="#FF181818" Foreground="White" BorderBrush="#FF181818">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="A" Width="120" DisplayMemberBinding="{Binding A}" />
                    <GridViewColumn Header="B" Width="50" DisplayMemberBinding="{Binding B}" />
                    <GridViewColumn Header="C" Width="150" DisplayMemberBinding="{Binding C}" />
                    <GridViewColumn Header="D" Width="150" DisplayMemberBinding="{Binding D}" />
                </GridView>
            </ListView.View>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#282828"/>
                                </Trigger>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="Background" Value="#333333" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView>

Any help is much appreciated. Thanks!

Upvotes: 1

Views: 797

Answers (1)

NoName
NoName

Reputation: 8025

You set ControlTemplate but don't have any child control to display data. And the style you need to modify is ListViewItem, not ListBoxItem.

You can do like this:

<ListView x:Name="TestListView" Margin="0,0.333,0.333,0" Grid.Row="1" Grid.Column="1" SelectedIndex="0" SelectionMode="Single" Background="#FF181818" Foreground="White" BorderBrush="#FF181818">
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="A" Width="120" DisplayMemberBinding="{Binding A}" />
            <GridViewColumn Header="B" Width="50" DisplayMemberBinding="{Binding B}" />
            <GridViewColumn Header="C" Width="150" DisplayMemberBinding="{Binding C}" />
            <GridViewColumn Header="D" Width="150" DisplayMemberBinding="{Binding D}" />
        </GridView>
    </ListView.View>
</ListView>

Upvotes: 1

Related Questions