当鼠标悬停在我的 ComboBox 上时,我的 ComboBox 背景出现了一对糟糕的蓝色/浅蓝色。 我在这里尝试了解决方案:ComboBox Mouse over color , WPF Combobox Mouse Over , How to style ComboBox Background on Mouse Hover?或 WPF combobox default hover color on togglebutton , 但它没有改变任何东西,我在悬停时仍然获得默认颜色。
有什么建议吗?
先谢谢大家了 德马西亚多。
这是 XAML 代码:
<Window x:Class="Homepage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window.Resources>
<Storyboard x:Key="TileZoomIn">
<ThicknessAnimation Storyboard.TargetProperty="Margin" From="10" To="1" Duration="0:0:0.1"/>
</Storyboard>
<Storyboard x:Key="TileZoomOut">
<ThicknessAnimation Storyboard.TargetProperty="Margin" From="1" To="10" Duration="0:0:0.1"/>
</Storyboard>
<DropShadowEffect x:Key="DropShadowEffect" BlurRadius="20" Opacity="1" ShadowDepth="0" Color="White"/>
</Window.Resources>
<Grid ShowGridLines="True">
<ComboBox Name="comboBoxTRIG" FontSize="40" Width="210" Height="98" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="40,-180,0,256" Background="Transparent" BorderBrush="Transparent" Foreground="White" BorderThickness="0">
<ComboBox Margin="25" Width="130" Height="50">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
</ComboBox.Resources>
</ComboBox>
</ComboBox>
</Grid>
</Window>
最佳答案
您的问题来自 ToggleButton 模板中的 ButtonChrome。从 ToggleButton 中删除它。
ComboBox -> ToggleButton -> ButtonChrome
步骤:
1) 打开 Expression Blend 并编辑 ComboBox 的 Style 副本,这将为您提供 ComboBox 的 Style + 它的 Template 及其所有 TemplateParts ,其中包括有问题的 ToggleButton。
2) 找到 ToggleButton,它的Style 名为“ComboBoxReadonlyToggleButton”。
3) 在“ComboBoxReadonlyToggleButton”中将 Themes:ButtonChrome 替换为 Border(如下面第 3 个代码块所示。)
ComboBox 的默认模板(已简化!):
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid" SnapsToDevicePixels="true">
<Popup x:Name="PART_Popup">
.....
</Popup>
<ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
<ContentPresenter ... />
</Grid>
</ControlTemplate>
切换按钮样式 + 模板(已简化!)。
<Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Themes:ButtonChrome x:Name="Chrome" ....>
<Grid>
<Path x:Name="Arrow" />
</Grid>
</Themes:ButtonChrome>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
您需要做的是覆盖默认的 ComboBox 模板并通过将 ButtonChrome 替换为 Border 来编辑切换按钮的样式:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<Grid>
<Path x:Name="Arrow" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
关于C# WPF ComboBox 鼠标悬停在颜色上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647730/