.net - 从 ListViewItem 中删除高亮效果

标签 .net wpf xaml styles itemcontainerstyle

ListViewListviewItems当鼠标悬停在它们上方或被选中时,它们不得改变外观。

我试图用这种风格来实现这一点,并取得了一些成功:

<Style x:Key="ItemContainerStyle1" TargetType="ListViewItem">
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Focusable" Value="False" />
        </Trigger>
    </Style.Triggers>
</Style>

但它提出了一个新问题。当背景设置为“透明”时,当鼠标悬停在 ListView 项上时,我现在可以看到下图所示的悬停/光泽效果。

enter image description here

我试图通过这次尝试解决问题,但没有运气。
<Style TargetType="{x:Type ListViewItem}">
    <Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000"/>
    </Style.Resources>
</Style>

任何人都知道如何消除这种悬停效果?

最佳答案

我不知道这是否是唯一的解决方案,但我这样做了(通过设置 Template s 的 ListViewItem 属性):

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border
                         BorderBrush="Transparent"
                         BorderThickness="0"
                         Background="{TemplateBinding Background}">
                        <GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListView.ItemContainerStyle>

编辑:

或者正如格兰特温尼所建议的那样(我自己没有测试过):

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListView.ItemContainerStyle>

关于.net - 从 ListViewItem 中删除高亮效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477552/

相关文章:

c# - 无法在 Windows 7 x64 上正确注册 x64 COM dll

.net - System.IO.Compression Zip 不使用 UTC

c# - Wpf,未应用样式

c# - 键盘事件未在 RT 应用程序中触发

c# - C# 中 TextBlock 的 WPF 最大宽度

.net - .NET 中 Web 服务的正确命名约定是什么?

c# - 在 C# 中读取 shapefile 坐标

.net - Wpf ListBoxes' ItemsSource 奇怪的行为

c# - WPF TabItem HeaderTemplate

c# - 在 XAML 中显示格式化文本