gpt4 book ai didi

c# - 更改 WPF ListViewItem 的选择颜色

转载 作者:可可西里 更新时间:2023-11-01 08:02:14 25 4
gpt4 key购买 nike

我有一个包含一些 ListViewItems 的 ListView。默认情况下,选择项目会使它们的背景变成深蓝色。我想应用一种样式,这样选择一个项目不会改变它的外观。

在我的 xaml 中我已经实现了这个:

<Style TargetType="ListViewItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</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>



<ListView x:Name="lvTestRun" ItemsSource="{Binding Path=TestRunData}" Margin="177,26,8,53.277" SelectionMode="Multiple">
<ListView.View>
<GridView>
<GridViewColumn Header="Status"/>
<GridViewColumn Header="Duration"/>
<GridViewColumn Header="Start Time"/>
<GridViewColumn Header="End Time"/>
</GridView>
</ListView.View>
</ListView>

为了重现这一点,您可能必须将单个 ListViewItem 的背景颜色设置为绿色或红色。

最佳答案

我不确定我是否理解您所说的白色而不是蓝色是什么意思。选择 ListViewItem 时,前景和背景将由触发器更改。要更改 ListViewItem 的前景,您可以使用此

<ListView ...>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<!-- Foreground for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black"/>
<!-- Background for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
...
</ListView>

更新

试试这个重新模板化。我删除了选定的所有触发器

<ListView ...>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<LinearGradientBrush x:Key="ListItemHoverFill" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF1FBFF" Offset="0"/>
<GradientStop Color="#FFD5F1FE" Offset="1"/>
</LinearGradientBrush>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border CornerRadius="2" SnapsToDevicePixels="True"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<Border Name="InnerBorder" CornerRadius="1" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition MaxHeight="11" />
<RowDefinition />
</Grid.RowDefinitions>

<Rectangle Name="UpperHighlight" Visibility="Collapsed" Fill="#75FFFFFF" />
<GridViewRowPresenter Grid.RowSpan="2"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ListItemHoverFill}" />
<Setter Property="BorderBrush" Value="#FFCCF0FF" />
<Setter TargetName="UpperHighlight" Property="Visibility" Value="Visible" />
</Trigger>
<!--<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource ListItemSelectedFill}" />
<Setter Property="BorderBrush" Value="#FF98DDFB" />
<Setter TargetName="InnerBorder" Property="BorderBrush" Value="#80FFFFFF" />
<Setter TargetName="UpperHighlight" Property="Visibility" Value="Visible" />
<Setter TargetName="UpperHighlight" Property="Fill" Value="#40FFFFFF" />
</Trigger>-->
<!--<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource ListItemSelectedInactiveFill}" />
<Setter Property="BorderBrush" Value="#FFCFCFCF" />
</MultiTrigger>-->
<!--<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource ListItemSelectedHoverFill}" />
<Setter Property="BorderBrush" Value="#FF98DDFB" />
</MultiTrigger>-->
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>

关于c# - 更改 WPF ListViewItem 的选择颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4246795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com