【问题标题】:TopAppBar button AutomationProperties styling in a Windows Store appWindows Store 应用程序中的 TopAppBar 按钮 AutomationProperties 样式
【发布时间】:2023-03-10 14:32:01
【问题描述】:

是否可以为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>

有人有想法吗?

【问题讨论】:

    标签: c# xaml windows-8 visual-studio-2012 windows-store-apps


    【解决方案1】:

    要实现这一点,您需要修改您的按钮样式所基于的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 中更改它们

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 2021-07-09
      相关资源
      最近更新 更多