ListView 项目控制模板触发器未触发

ListView Item Control Template triggers not firing

我有一个包含 GridView 的 ListView。我正在尝试同时更改所选项目的背景和悬停的项目的背景。经过一番搜索,我发现了控制模板与触发器的结合,但它们没有触发。

这是我的意思的截图

<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>

非常感谢任何帮助。谢谢!

您设置了 ControlTemplate 但没有任何子控件来显示数据。而且需要修改的样式是ListViewItem,不是ListBoxItem.

你可以这样做:

<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>