XAML - 想要更改seachbox的搜索图标颜色

时间:2017-08-17 16:39:41

标签: c# xaml

这是我的搜索框标签&我想将他的图标颜色从紫色(默认颜色)更改为黑色

<SearchBox Height="50" Width="800" Margin="0,0,0,0" ></SearchBox>

enter image description here我是Xaml的乞丐

2 个答案:

答案 0 :(得分:0)

假设您在Visual Studio中使用通用Windows应用程序,请在设计查看器中选择文本框,然后右键单击以打开菜单。在菜单中,选择“编辑模板”和“编辑副本”。这将为您提供选择搜索框部分的选项,例如按钮以编辑背景。在生成的App.xaml代码中找到此代码以开始使用:

<Button x:Name="SearchButton" AutomationProperties.AccessibilityView="Raw" Background="#FFB49494" Grid.Column="1" FontWeight="{ThemeResource SearchBoxButtonThemeFontWeight}" Style="{StaticResource SearchButtonStyle}"/>

答案 1 :(得分:0)

转到 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>