gpt4 book ai didi

c# - 设置背景颜色或 WPF (4.0) ListBox - Windows 8

转载 作者:IT王子 更新时间:2023-10-29 04:52:13 25 4
gpt4 key购买 nike

我试图将所选 ListBoxItem 的背景颜色设置为白色而不是系统颜色。我已经阅读了我可以在此处找到的有关 SO 的内容,并已遵循或相信已遵循那里的建议( Change background color for selected ListBox itemWPF How to change the listbox selected item text color when the list box loses focusChange selected and unfocused Listbox style to not be grayed out 等)。

所有似乎都通过将所选项目的 HighlightBrush 和 ControlBrush 设置为透明来解决问题。我有以下 XAML,它正确设置了字体颜色,但无论画笔设置如何,背景都是默认的透明蓝色。我仍然是一个 WPF 菜鸟,所以我一定错过了一些简单的东西。

<ListBox Width="Auto" Height="Auto" Grid.Column="0" BorderThickness="0" Background="#FFF3F3F3" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ListBox.ItemsSource>
<x:Array Type="{x:Type sys:String}">
<sys:String>String 1</sys:String>
<sys:String>String 2</sys:String>
<sys:String>String 3</sys:String>
<sys:String>String 4</sys:String>
</x:Array>
</ListBox.ItemsSource>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</Style.Resources>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="#999999"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Right" Margin="0,0,8,0" Background="Transparent"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

我将不胜感激任何朝着正确方向的插入。

编辑:

在阅读了第一个答案后,我发现它对他们稍作改动后很有效,我将我一直在我的 Windows 8 机器上开发的应用程序并在 Windows 7 VM 中执行它,它按预期工作。关于需要更改哪些内容才能使其在 Windows 8 机器和 Windows 7 上运行,您有什么想法吗?

最佳答案

这些帖子对于 Windows-8 来说已经过时了。

在 Windows-8 中,出于某种原因,Microsoft 不希望人们如此轻松地编辑他们的默认 Style 或使用 Brush 覆盖的东西。

ListBoxItem 来自 VS 的默认 Style 有这个用于选择触发器:

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="False" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="#3DDADADA" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="#FFDADADA" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="True" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="#3D26A0DA" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="#FF26A0DA" />
</MultiTrigger>

选择状态的触发器不再应用我们可以轻松覆盖的画笔,而是静态颜色。因此,要修改它,您将需要派生模板并在那里修改触发器。到白色

这是 VS2012 Windows-8 为 ListBoxItem 提供的完整样式

<Style x:Key="ListBoxItemStyle1"
TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="Padding"
Value="4,1" />
<Setter Property="HorizontalContentAlignment"
Value="{Binding HorizontalContentAlignment,
RelativeSource={RelativeSource FindAncestor,
AncestorLevel=1,
AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment"
Value="{Binding VerticalContentAlignment,
RelativeSource={RelativeSource FindAncestor,
AncestorLevel=1,
AncestorType={x:Type ItemsControl}}}" />
<Setter Property="Background"
Value="Transparent" />
<Setter Property="BorderBrush"
Value="Transparent" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2"
SnapsToDevicePixels="True"
Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
StrokeDashArray="1 2"
StrokeThickness="1" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="True">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="#1F26A0DA" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="#A826A0DA" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="False" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="#3DDADADA" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="#FFDADADA" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="True" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="#3D26A0DA" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="#FF26A0DA" />
</MultiTrigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter TargetName="Bd"
Property="TextElement.Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

如果您将这些触发器修改为:

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="False" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="White" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="White" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="True" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd"
Property="Background"
Value="White" />
<Setter TargetName="Bd"
Property="BorderBrush"
Value="White" />
</MultiTrigger>

你的问题应该已经解决了。

关于c# - 设置背景颜色或 WPF (4.0) ListBox - Windows 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16819577/

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