ListView项控制模板触发不触发

时间:2016-03-05 02:53:28

标签: c# wpf listview highlight listviewitem

我有一个包含GridView的ListView。我正在尝试更改所选项目的背景以及悬停在其上的项目的背景。经过一些搜索,我遇到了控制模板和触发器,但它们没有被触发。

这是我的意思截图 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>

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

1 个答案:

答案 0 :(得分:1)

您设置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>