在WPF应用程序中,我需要设置组合框的背景颜色。
如附件所示,我将背景色设置为蓝色(在后面的代码中):
_combobox.Background = Brushes.DodgerBlue;
我还设置了触发器,以便在选择项目时处理事件(为ComboBox和ComboBoxItem设置样式:
<Style x:Key="CmbStyleItem" TargetType="{x:Type ComboBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="gd" Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=Background}" Padding="4,6,2,2">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ComboBoxItem.IsMouseOver" Value="True">
<Setter TargetName="gd" Property="Background" Value="#E7E2E2" />
<Setter TargetName="gd" Property="TextElement.Foreground" Value="#000000"/>
</Trigger>
<Trigger Property="ComboBoxItem.IsSelected" Value="True">
<Setter TargetName="gd" Property="Background" Value="#D6D6D6" />
<Setter TargetName="gd" Property="TextElement.Foreground" Value="#000000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="27" />
<Setter Property="Padding" Value="4,6,2,2" />
<Setter Property="FontFamily" Value="{StaticResource fntConsole}" />
<Setter Property="Typography.Capitals" Value="AllSmallCaps"/>
<Setter Property="FontSize" Value="13.55" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="ItemContainerStyle" Value="{DynamicResource CmbStyleItem}"/>
</Style>
基本上,我无法设置所选项目的背景颜色,例如在附件中,项目“ Tav1800x650x18”。有提示吗?
答案 0 :(得分:0)
如果我很想知道您的问题,那么您想在没有selecteditem时修改组合框的背景色:
所以您有这行<Setter Property="Background" Value="Transparent" />
给出默认的背景颜色
您必须用所需的颜色设置Value
。
否则我会误会你.....
答案 1 :(得分:0)
替换: Background =“ {Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ComboBox}},Path = Background}”
收件人:Background =“ {TemplateBinding背景}”