从自定义 WPF ListView 中删除选择突出显示
Remove selection highlight from custom WPF ListView
我有一个 ListView
和自定义 ItemTemplate
的项目。
我想删除或更改选择的视觉效果。
到目前为止,我尝试将自定义 ItemContainerStyle
分配给我的 ListView
:
<ListView x:Name="DispList" ItemContainerStyle="{StaticResource MySty}" ItemTemplate="{StaticResource Mine}">
</ListView>
并且在resources下,定义样式如下:
<Style TargetType="{x:Type ListViewItem}" x:Key="MySty">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
不幸的是,这不起作用。我错过了什么?
这种尝试覆盖系统颜色的方法不适用于 Windows 8 及更高版本。
您需要修改ListViewItem容器的ControlTemplate。
参考:
一个例子:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="RootBorder">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="RootBorder" Property="BorderBrush" Value="Red"/>
<Setter TargetName="RootBorder" Property="BorderThickness" Value="1"/>
</Trigger>
<!--<Trigger Property="IsSelected" Value="True">
<Setter TargetName="RootBorder" Property="Background" Value="LightBlue"/>
</Trigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListViewItem>123</ListViewItem>
<ListViewItem>456</ListViewItem>
<ListViewItem>789</ListViewItem>
</ListView>
我有一个 ListView
和自定义 ItemTemplate
的项目。
我想删除或更改选择的视觉效果。
到目前为止,我尝试将自定义 ItemContainerStyle
分配给我的 ListView
:
<ListView x:Name="DispList" ItemContainerStyle="{StaticResource MySty}" ItemTemplate="{StaticResource Mine}">
</ListView>
并且在resources下,定义样式如下:
<Style TargetType="{x:Type ListViewItem}" x:Key="MySty">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
不幸的是,这不起作用。我错过了什么?
这种尝试覆盖系统颜色的方法不适用于 Windows 8 及更高版本。 您需要修改ListViewItem容器的ControlTemplate。
参考:
一个例子:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="RootBorder">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="RootBorder" Property="BorderBrush" Value="Red"/>
<Setter TargetName="RootBorder" Property="BorderThickness" Value="1"/>
</Trigger>
<!--<Trigger Property="IsSelected" Value="True">
<Setter TargetName="RootBorder" Property="Background" Value="LightBlue"/>
</Trigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListViewItem>123</ListViewItem>
<ListViewItem>456</ListViewItem>
<ListViewItem>789</ListViewItem>
</ListView>