用于组合框模板的 WPF 高亮画笔
Posted
技术标签:
【中文标题】用于组合框模板的 WPF 高亮画笔【英文标题】:WPF Highlight brush for Combobox template 【发布时间】:2019-07-29 15:17:16 【问题描述】:我不知道如何将组合框的突出显示画笔(将在鼠标悬停时标记正在选择的项目的颜色)设置为某个画笔。我使用编辑模板-> 复制来获取我尝试设置 SystemColors.HighlightBrushKey 的模板的副本。我已经看到了应该能够通过在样式资源中这样定义画笔来设置画笔的答案,但它只是不起作用。
我也尝试设置放置组合框的网格的 Style.Resource,但这也没有帮助。
请参见我将颜色设置为红色的第 3 行和第 15 行。没有效果。我做错了什么?
<Style x:Key="TeachpendantVMVisionServerComboBoxStyle" TargetType="x:Type ComboBox">
<Style.Resources>
<SolidColorBrush x:Key="x:Static SystemColors.HighlightBrushKey" Color="Red" />
<SolidColorBrush x:Key="x:Static SystemColors.ControlBrushKey" Color="Red" />
</Style.Resources>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type ComboBox">
<Grid x:Name="grid">
<Grid.Style>
<Style>
<Style.Resources>
<SolidColorBrush x:Key="x:Static SystemColors.HighlightBrushKey" Color="Red" />
<SolidColorBrush x:Key="x:Static SystemColors.ControlBrushKey" Color="Red" />
</Style.Resources>
</Style>
</Grid.Style>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition MaxWidth="18"/>
</Grid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid"/>
<VisualState x:Name="InvalidFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="PART_EditableTextBox">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Red"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="path">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource MoveTextBox.Invalid.BorderBrush"/>
</ObjectAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="toggleButton">
<EasingColorKeyFrame KeyTime="0" Value="#FFFB0000"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="InvalidUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="PART_EditableTextBox">
<DiscreteObjectKeyFrame KeyTime="0" Value="StaticResource MoveTextBox.Invalid.BorderBrush"/>
</ObjectAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="toggleButton">
<EasingColorKeyFrame KeyTime="0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="path">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Red"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="PART_EditableTextBox"
Padding="5,0,0,0"
Height="TemplateBinding Height">
<TextBox.Template>
<ControlTemplate TargetType="x:Type TextBox">
<Grid>
<Border
x:Name="border"
CornerRadius="3,0,0,3"
BorderThickness="1,1,0,1"
Background="DynamicResource Button.Static.Background"
BorderBrush="DynamicResource Button.Static.Background">
<ScrollViewer x:Name="PART_ContentHost"/>
</Border>
</Grid>
</ControlTemplate>
</TextBox.Template>
</TextBox>
<ToggleButton x:Name="toggleButton" Grid.Column="1" Margin="0"
Height="TemplateBinding Height"
Focusable="False"
IsChecked="Binding IsDropDownOpen, Mode=TwoWay, RelativeSource=RelativeSource TemplatedParent"
ClickMode="Press" BorderBrush="#FFABADB3">
<ToggleButton.Template>
<ControlTemplate TargetType="x:Type ToggleButton">
<Border
Background="DynamicResource Button.Checked.Background"
x:Name="border"
CornerRadius="0,3,3,0"
BorderThickness="0,1,1,1"
BorderBrush="DynamicResource Button.Static.Background">
<ContentPresenter />
</Border>
</ControlTemplate>
</ToggleButton.Template>
<Path x:Name="path" Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z"
Fill="Gold" />
</ToggleButton>
<ContentPresenter x:Name="ContentSite"
Content="TemplateBinding SelectionBoxItem"
ContentTemplate="TemplateBinding SelectionBoxItemTemplate"
ContentTemplateSelector="TemplateBinding ItemTemplateSelector"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Margin="5,0,0,0" RecognizesAccessKey="True"/>
<Popup x:Name="Popup"
Placement="Bottom"
IsOpen="TemplateBinding IsDropDownOpen"
AllowsTransparency="True"
Focusable="True"
PopupAnimation="Slide"
OverridesDefaultStyle="True">
<Grid x:Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="TemplateBinding ActualWidth"
MaxHeight="TemplateBinding MaxDropDownHeight">
<Border
x:Name="DropDownBorder"
BorderThickness="1"
CornerRadius="5"
Background="Azure"
BorderBrush="DarkGray">
</Border>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate/>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
【参考方案1】:毕竟它相当简单(但在哪里可以找到它并不明显)。 只需编辑 ItemsContainerStyle 的副本,所有这些画笔都可以从那里设置。
更改 ComboBox 的布局(创建圆形组合框或更改颜色) Style="DynamicResource MyComboBoxStyle"
更改悬停和选定的突出显示颜色
ItemContainerStyle="DynamicResource MyComboBoxItemS"
完整的组合框在 XAML 中看起来像这样
<ComboBox x:Name="positionsComboBox"
Grid.Column="1"
Grid.ColumnSpan="3"
IsReadOnly = "True"
IsEditable = "False"
Margin="3,0,3,0"
ItemsSource="Binding IDs, Source=StaticResource Locator, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay"
SelectedValue="Binding SelectedID, Source=StaticResource Locator, Mode=TwoWay"
Style="DynamicResource MyComboBoxStyle"
IsSynchronizedWithCurrentItem="True"
ItemContainerStyle="DynamicResource MyComboBoxItemContainerStyle"
FontWeight="Bold"
FontSize="24"/>
【讨论】:
以上是关于用于组合框模板的 WPF 高亮画笔的主要内容,如果未能解决你的问题,请参考以下文章