我需要设置可编辑的背景颜色combobox
在代码中。这是我所拥有的,但不改变颜色:
ComboBox comboBox = sender as ComboBox;
comboBox.Background = Brushes.PeachPuff;
if (comboBox.IsEditable == true)
{
TextBox textBox = (TextBox)comboBox.Template.FindName("PART_EditableTextBox", comboBox);
if (textBox != null)
{
textBox.Background = Brushes.PeachPuff;
}
}
我原以为背景颜色会变成 PeachPuff(浅橙色),但什么也没发生 - 有什么想法吗?
改变combobox
's background
使用background
属性仅适用于 Win7 及更早版本,在 Windows 8 及更高版本中使用默认模板ComboBox
已更改,要修复您应该编辑默认模板的问题,
- using 视觉工作室2013 or Blend,右键单击
combobox
并选择 EditTemplate > 编辑副本:
-
在生成的 Xaml 中搜索<ControlTemplate TargetType="{x:Type ToggleButton}">
并替换{StaticResource ComboBox.Static.Background}
标记为TemplateBinding
to the Background
属性,更新后您的代码应如下所示:
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="templateRoot" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
<Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
</Border>
</Border>
<ControlTemplate.Triggers>
<MultiDataTrigger>
...
-
现在,您可以使用背景属性来更改Combobox
color:
<Grid>
<ComboBox IsEditable="True" x:Name="EditableComboBox" Background="PeachPuff" VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Style="{DynamicResource ComboBoxStyle1}" >
</ComboBox>
</Grid>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)