【发布时间】:2021-06-30 17:13:36
【问题描述】:
这是我的搜索框标签,我想将他的图标颜色从紫色更改为黑色(默认颜色)
<SearchBox Height="50" Width="800" Margin="0,0,0,0" ></SearchBox>
【问题讨论】:
-
控件从何而来?我不知道内置 WPF 中有一个。 -
是的,是内置 wpf 中的那个
这是我的搜索框标签,我想将他的图标颜色从紫色更改为黑色(默认颜色)
<SearchBox Height="50" Width="800" Margin="0,0,0,0" ></SearchBox>
【问题讨论】:
假设您在 Visual Studio 中使用通用 Windows 应用程序,请在设计查看器中选择您的文本框并右键单击以打开菜单。在菜单中,选择“编辑模板”和“编辑副本”。这将使您可以选择部分搜索框,例如编辑背景的按钮。在生成的App.xaml 代码中找到此代码即可开始使用:
<Button x:Name="SearchButton" AutomationProperties.AccessibilityView="Raw" Background="#FFB49494" Grid.Column="1" FontWeight="{ThemeResource SearchBoxButtonThemeFontWeight}" Style="{StaticResource SearchButtonStyle}"/>
【讨论】:
转到 UI 编辑器,右键单击控件并选择“编辑模板”和“编辑副本”。现在将样式代码放置在您想要的位置,将 Style="{StaticResource YourStyleName}" 放入 SearchBox 的 XAML 代码中,并在您的样式中更改以下部分。这里有不同的“VisualStates”。 “PointOver”的状态已经存在。在这里,您可以更改 i 的值。 e“SearchBoxButtonPointerOverForegroundThemeBrush”直接或创建一个新的画笔覆盖属性。对于“正常”状态,必须复制和调整 StoryBoard 条目。
我建议创建一个 SolidColorBrush 来覆盖 ThemeResource。
<SolidColorBrush x:Key="SearchBoxButtonPointerOverBackgroundThemeBrush" Color="Gray"/>
<SolidColorBrush x:Key="SearchBoxButtonPointerOverForegroundThemeBrush" Color="White"/>
<SolidColorBrush x:Key="SearchBoxButtonBackgroundThemeBrush" Color="White"/>
<SolidColorBrush x:Key="SearchBoxButtonForegroundThemeBrush" Color="Black"/>
之前:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchGlyph" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SearchBoxButtonPointerOverForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchButtonBackground" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SearchBoxButtonPointerOverBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
之后:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchGlyph" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SearchBoxButtonForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchButtonBackground" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SearchBoxButtonBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchGlyph" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SearchBoxButtonPointerOverForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="SearchButtonBackground" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SearchBoxButtonPointerOverBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
【讨论】: