为什么我的背景颜色是在组合中工作的

问题描述 投票:0回答:3
我组合盒的唯一部分是背景颜色。我希望整个东西变黄。但是倒塌的部分仍然只是灰色。

<ComboBox Height="25" Width="125" Background="Yellow"> <ComboBox.ItemContainerStyle> <Style TargetType="ComboBoxItem"> <Setter Property="Background" Value="Yellow"/> <Setter Property="BorderBrush" Value="Yellow"/> </Style> </ComboBox.ItemContainerStyle> <ComboBoxItem Content="One"/> <ComboBoxItem Content="Two"/> <ComboBoxItem Content="Three"/> </ComboBox>

	
c# wpf combobox
3个回答
12
投票

<ComboBox Height="25" Width="125" Background="Yellow" Style="{StaticResource {x:Static ToolBar.ComboBoxStyleKey}}"> <ComboBox.ItemContainerStyle> <Style TargetType="ComboBoxItem"> <Setter Property="Background" Value="Yellow"/> <Setter Property="BorderBrush" Value="Yellow"/> </Style> </ComboBox.ItemContainerStyle> <ComboBoxItem Content="One"/> <ComboBoxItem Content="Two"/> <ComboBoxItem Content="Three"/> </ComboBox>


在使用Windows 7的WPF时,您的代码应按预期工作。但是,如果您正在运行Windows 8或更高版本,则可能不适用该Windows 8。 

3
投票
https://blog.magnusmontin.net/2014/04/04/04/30/changing-the-background-colour-colour-of-a-combobox-in-wpf-on-wpf-on-windows-8/-可能会帮助您实现什么你想要

作者对此做出了很好的解释。

我在Windows 11中以代码完成了这一点:

for (int s=0; (s < ItemContainerStyle.Setters.Count); s++) if (((Setter)ItemContainerStyle.Setters[s]).Property == IsEnabledProperty) { ItemContainerStyle.Setters.RemoveAt(s); break; } ItemContainerStyle.Setters.Add(new Setter(BackgroundProperty,Brushes.Yellow));

它删除了ISENABLE属性的设置器,这是负责保持Combobox背景灰色的原因。 然后,它添加了一个新的设置器,以使背景为黄色。

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.