【问题标题】:Change foreground color of back button in UWP NavigationView在 UWP NavigationView 中更改后退按钮的前景色
【发布时间】:2021-04-28 01:30:34
【问题描述】:

我尝试更改 UWP NavigationView 中后退按钮的前景色。我设法使用以下资源更改了侧边菜单中的所有其他项目:

    <SolidColorBrush x:Key="NavigationViewItemForegroundPointerOver" Color="White"/>
    <SolidColorBrush x:Key="NavigationViewItemForegroundSelected" Color="White"/>
    <SolidColorBrush x:Key="NavigationViewItemForegroundSelectedPointerOver" Color="White"/>
    <SolidColorBrush x:Key="NavigationViewItemForegroundPressed" Color="White"/>
    <SolidColorBrush x:Key="NavigationViewItemForegroundSelectedPressed" Color="White"/>
    <SolidColorBrush x:Key="NavigationViewItemForeground" Color="White" />

    <Style x:Key="CustomNavigationMenuStyle" TargetType="Button" BasedOn="{StaticResource PaneToggleButtonStyle}">
        <Setter Property="Foreground" Value="White" />
    </Style>

我必须为后退按钮覆盖哪些资源?

【问题讨论】:

    标签: uwp uwp-xaml winui


    【解决方案1】:

    要更改NavigationView的后退按钮的样式,可以先查看NavigationView的样式。

    在 Visual Studio 中的 Document Outline Window 中找到 NavigationView control。然后右键单击控件,移动到 Edit Template 并选择 Edit a Copy。之后,将创建 NavigationView 的默认样式。您可以找到一种名为 NavigationBackButtonNormalStyle 的样式。这是后退按钮的默认样式。您可以根据需要更改 Foreground 属性。

    像这样:

            <Style x:Key="NavigationBackButtonNormalStyle" TargetType="Button">
            <Setter Property="Background" Value="{ThemeResource NavigationViewBackButtonBackground}"/>
            <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
            <Setter Property="FontFamily" Value="{ThemeResource SymbolThemeFontFamily}"/>
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="Height" Value="{ThemeResource NavigationBackButtonHeight}"/>
            <Setter Property="Width" Value="{ThemeResource NavigationBackButtonWidth}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}"/>
            <Setter Property="Content" Value="&#xE72B;"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NavigationViewButtonBackgroundPointerOver}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NavigationViewButtonForegroundPointerOver}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NavigationViewButtonBackgroundPressed}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NavigationViewButtonForegroundPressed}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Content" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NavigationViewButtonForegroundDisabled}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <FontIcon x:Name="Content" AutomationProperties.AccessibilityView="Raw" FontFamily="{TemplateBinding FontFamily}" Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" Glyph="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" MirroredWhenRightToLeft="True" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    【讨论】:

    • 行得通,谢谢!我只是希望,有办法不复制整个风格。
    猜你喜欢
    • 2019-10-25
    • 2016-07-04
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2019-07-09
    • 2022-01-01
    相关资源
    最近更新 更多