在我的WPF应用程序中,我只想更改组合框的背景颜色.我不是指下拉,我想要的只是选择背景设置的任何项目.就像设置按钮的背景一样 - 当控件显示在屏幕上时,它应该具有LightYellow背景.而已.我在网上搜索了很多,但到处都可以找到下拉背景颜色的解决方案.我尝试将SolidColorBrush和Style.Triggers应用到Combobox的TextBlock,但没有成功.通过添加SolidColorBrush线,我得到了我的下拉背景设置,但这不是我想要的.我的代码是:
<ComboBox ItemsSource="{Binding MtrCm}" SelectedValue="{Binding WellboreDiameter_Unit, Mode=TwoWay}" Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,26,249,0" x:Name="cboWellDiameter" VerticalAlignment="Top" Width="120" Background="LightYellow" >
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="Yellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Yellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" />
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Resources>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我设置他正在寻找的所需组件的背景.
谢谢
我试图将所选ListBoxItem的背景颜色设置为白色而不是系统颜色.我已经阅读了我在这里可以找到的内容并且已经跟随或者相信已经遵循了那里的建议(更改所选ListBox项目的背景颜色,WPF如何在列表框失去焦点时更改列表框所选项目文本颜色,更改选中和不专心的列表框样式不会变灰,以及其他).
所有似乎都通过将HighlightBrush和ControlBrush设置为所选项目的透明来解决问题.我有以下XAML并且它正确设置了字体颜色,但无论画笔设置如何,backgroound都是默认的透明蓝色.我仍然是一个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" …Run Code Online (Sandbox Code Playgroud)