【问题标题】:XAML - want to change seachbox's search icon colorXAML - 想要更改搜索框的搜索图标颜色
【发布时间】:2021-06-30 17:13:36
【问题描述】:

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

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

我是 Xaml 的初学者

【问题讨论】:

  • 控件从何而来?我不知道内置 WPF 中有一个。
  • 是的,是内置 wpf 中的那个

标签: c# xaml


【解决方案1】:

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

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

【讨论】:

  • 它不起作用,我的意思是它显示颜色正在改变,但是当我启动应用程序时它再次显示默认颜色
  • 您想在 'Background="transparent"' 类别中使用自定义颜色,而不是 'transparent'。根据你发给我的代码,现在它是透明的。你用来使它变成紫色的 XAML 是什么?
  • 好吧,基本上我用这个代码 紫色是他的默认颜色跨度>
  • 默认颜色实际上是透明的,就像在您的第一条评论中一样,因此您的应用程序或模板中的一些其他代码将覆盖默认模板颜色,如果这有意义的话。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多