WPF:在选择或鼠标悬停时更改列表视图项的背景/前景

mspsb9vt  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(146)

我的WPF程序中有一个列表视图,我想在将鼠标悬停在列表视图项目上或选中列表视图项目时更改其样式
这是我目前使用的代码:

<Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="#1A1A1A"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </Trigger>

                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderThickness" Value="0"/>
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="#1A1A1A"/>
                </Trigger>
            </Style.Triggers>
        </Style>

问题是,它只改变了边框粗细或字体粗细,背景和前景根本没有改变
我在网上找不到任何有用的东西
悬停时的列表视图项目:

列表视图项目(选定时):

我尝试过使用这种方法-使用我自己的颜色值

<Window.Resources>
    <Style TargetType="ListViewItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>                      
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
        </Style.Resources>
    </Style>
</Window.Resources>

然后我也试过禁用可聚焦-这将删除选择高亮,但也删除了不应该发生的功能

ovfsdjhp

ovfsdjhp1#

这是我想做的
删除了默认的悬停/选定效果,这样我就可以使用setter来制作自己的效果

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ListViewItem}">
            <Border Background="{TemplateBinding Background}">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

相关问题