从自定义WPF ListView中移除选择高亮显示(Remove selection highlight from custom WPF ListView)

编程入门 行业动态 更新时间:2024-10-28 08:22:55
自定义WPF ListView中移除选择高亮显示(Remove selection highlight from custom WPF ListView)

我有一个ListView与项目的自定义ItemTemplate 。 我想删除或更改视觉效果以供选择。

到目前为止,我尝试将一个自定义的ItemContainerStyle分配给我的ListView :

<ListView x:Name="DispList" ItemContainerStyle="{StaticResource MySty}" ItemTemplate="{StaticResource Mine}"> </ListView>

在资源下,定义风格如下:

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

不幸的是,这不起作用。 我错过了什么?

I have a ListView with custom ItemTemplate for the items. I want to remove or change the visual effect for selection.

So far I tried assgining a custom ItemContainerStyle to my ListView:

<ListView x:Name="DispList" ItemContainerStyle="{StaticResource MySty}" ItemTemplate="{StaticResource Mine}"> </ListView>

And under resources, defining the style as follows:

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

Unfortunately, this does not work. What am I missing?

最满意答案

尝试覆盖系统颜色的这种方法在Windows 8及更高版本上无效。 您需要修改ListViewItem容器的ControlTemplate。

请参阅: ListView选择的项目样式替代

一个例子:

<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>

This approach of trying to override the system colours doesn't work on Windows 8 and later. You need to modify the ControlTemplate of the ListViewItem container.

Refer to: ListView Selected Item Style Override

An example:

<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>

更多推荐

本文发布于:2023-08-07 10:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   移除   WPF   ListView   高亮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!