删除自定义 Combobox 样式 wpf 上的 MouseOver 效果
Delete MouseOver Effect on custom Combobox style wpf
在我的 wpf 文档中,我制作了一个自定义组合框样式,如下所示:
<ComboBox Background="#222222" BorderBrush="Black" Grid.Column="1" Height="30" Width="250">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#262626" />
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="#262626" /> </Trigger> </Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem Foreground="White" Name="Item1">Item1
</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item2">Item2</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item3">Item3</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item4">Item4</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item5">Item5</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item6">Item6</ComboBoxItem>
但是每当我将鼠标悬停在其中一个 ComboboxItem 上时,我仍然会获得默认的悬停颜色,那么如何删除它并放置我的颜色?
我对编码还比较陌生,所以如果能提供任何帮助,我们将不胜感激。
您必须覆盖 ComboBoxItem
的默认 ControlTemplate
才能覆盖默认的视觉状态触发器。
检查 Microsoft Docs: Control Styles and Templates 以查看框架控件的默认 Style
实现。
ComboBoxItem Style
<ComboBox>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="#262626" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
<Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
在我的 wpf 文档中,我制作了一个自定义组合框样式,如下所示:
<ComboBox Background="#222222" BorderBrush="Black" Grid.Column="1" Height="30" Width="250">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#262626" />
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="#262626" /> </Trigger> </Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem Foreground="White" Name="Item1">Item1
</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item2">Item2</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item3">Item3</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item4">Item4</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item5">Item5</ComboBoxItem>
<ComboBoxItem Foreground="White" Name="Item6">Item6</ComboBoxItem>
但是每当我将鼠标悬停在其中一个 ComboboxItem 上时,我仍然会获得默认的悬停颜色,那么如何删除它并放置我的颜色?
我对编码还比较陌生,所以如果能提供任何帮助,我们将不胜感激。
您必须覆盖 ComboBoxItem
的默认 ControlTemplate
才能覆盖默认的视觉状态触发器。
检查 Microsoft Docs: Control Styles and Templates 以查看框架控件的默认 Style
实现。
ComboBoxItem Style
<ComboBox>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="#262626" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
<Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>