我有一个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>更多推荐
发布评论