WPF:ListViewItem上的IsMouseOver触发器

时间:2018-04-01 15:50:55

标签: wpf xaml listview triggers listviewitem

我有以下代码:

    <Style>
        <Style TargetType="{x:Type ListViewItem}" x:Key="MyStyle">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>

当我使用这种样式在ListView上设置ItemContainerStyle属性时,它不起作用 - 后台不会改变。

我看到几个类似的问题,人们回答说它不起作用 一个ListView但没有人给出一个简单的响应如何创建这个触发器。

你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您需要更改listviewitem的模板才能实现此目的。 这是基于win10模板的标记工作:

    <ListView>
        <ListView.Resources>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate  TargetType="{x:Type ListViewItem}">
                            <Border x:Name="Bd" 
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="Transparent" 
                Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
                              Content="{TemplateBinding Content}" 
                              ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsMouseOver" Value="True"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="Red"/>
                                    <Setter Property="BorderBrush" TargetName="Bd" Value="#A826A0DA"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                        <Condition Property="IsSelected" Value="True"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="Orange"/>
                                    <Setter Property="BorderBrush" TargetName="Bd" Value="#FFDADADA"/>
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="True"/>
                                        <Condition Property="IsSelected" Value="True"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd" Value="Yellow"/>
                                    <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.Resources>

        <ListViewItem >AAAAAAAAAAAA</ListViewItem>
        <ListViewItem>BBBBBBBBBBBB</ListViewItem>
        <ListViewItem>CCCCCCCCCCCC</ListViewItem>
    </ListView>