在Windows Store应用中,如何为TopAppBar按钮设置AutomationProperties样式?

4

是否可以用不同的颜色样式化AutomationProperties.Name值?我从我的应用程序中的暗主题获取基本文本颜色。我有一个自定义背景颜色,这就是为什么我需要一个特定的ForegroundColorTextColor来设置此属性(Value="OtherUserAppBarButton")。

<Style x:Key="LogoutAppBarButtonStyle" TargetType="ButtonBase" 
              BasedOn="{StaticResource AppBarButtonStyle}">
    <Setter Property="AutomationProperties.AutomationId" Value="OtherUserAppBarButton"/>
    <Setter Property="AutomationProperties.Name" Value="Other User"/>
    <Setter Property="Content" Value="&#xE1A6;"/>
    <Setter Property="Foreground" Value="#ffffffff" />
</Style>

有人有想法吗?
1个回答

3
为了实现这一点,您需要修改基于其构建按钮样式的AppBarButtonStyle。您可以在项目中的Common\StandardStyles.xaml中找到它。您可以直接在此文件中修改样式,或者如果您还需要未修改的样式,则可以在App.xaml中创建其副本。
您需要更改样式的ControlTemplate中的以下块:
<TextBlock
    x:Name="TextLabel"
    Text="{TemplateBinding AutomationProperties.Name}"
    Foreground="{StaticResource AppBarItemForegroundThemeBrush}"
    Margin="0,0,2,0"
    FontSize="12"
    TextAlignment="Center"
    Width="88"
    MaxHeight="32"
    TextTrimming="WordEllipsis"
    Style="{StaticResource BasicTextStyle}"/>

如您所见,Foreground 属性被固定为 AppBarItemForegroundThemeBrush。您可以将其更改为 {TemplateBinding Foreground},以匹配您在 LogoutAppBarButtonStyle 中设置的颜色,或者在模板中直接使用其他自定义固定颜色。

此外,请不要忘记其他视觉状态(PointerOverPressedDisabledChecked)的样式。它们也设置为主题颜色。您可以在模板的 VisualStateManager 中更改它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接