【问题标题】:UWP: Redefining Button StylesUWP:重新定义按钮样式
【发布时间】:2021-05-14 19:06:33
【问题描述】:

我想通过定义明暗模式的样式来修改按钮的颜色。我的样式定义如下:

<Style x:Key="ExampleButton" TargetType="Button">
    <Setter Property="FontSize" Value="14" />
    <Setter Property="FontWeight" Value="Normal" />
</Style>

在另一个文件中,我已将 ButtonBackground 定义为浅色模式下的“红色”。我的期望是当我的应用程序使用这个 ExampleButton 时,它应该有一个红色背景。但是,我仍然可以使用它的默认 BorderBackground 看到这个按钮,即 SystemControlBackgroundBaseLowBrush。我可以通过 3 种方式解决这个问题:

  1. 将 SystemControlBackgroundBaseLowBrush 重新定义为“Red”,这可行,但我可能会将 SystemControlBackgroundBaseLowBrush 用于其他控件,所以不想使用这种方法。

  2. 在上面的代码中添加另一个 setter 并有类似的东西

     <Style x:Key="ExampleButton" TargetType="Button">
       <Setter Property="FontSize" Value="14" />
       <Setter Property="FontWeight" Value="Normal" />
       <Setter Property="Background" Value="Red"/>
    </Style>
    
  3. 编辑按钮模板中的值

实现这一目标的最优雅的解决方案是什么?除了我上面描述的3个之外,还有更好的解决方案吗?

我希望避免添加整个模板来修改颜色

【问题讨论】:

  • 你的进展如何?如果您的问题已经解决,您可以标记有用的答案,这将有助于其他面临相同问题的人。

标签: uwp uwp-xaml


【解决方案1】:

最好的方法确实是定义一个SolidColorBrush,它会因应用主题而异。首先,您应该像这样将ResourceDictionary.ThemeDictionaries 添加到您的Application.ResourcesPage.Resources。然后将此SolidColorBrush 设置为按钮样式的BackGround 属性的值,如下所示:

<Page
    ....
    RequestedTheme="Light">
    <!--Red button, or set to "Dark" for a blue button, remove this line or set to "Auto" to choose device theme-->
    
    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Light">
                    <SolidColorBrush x:Key="CustomButtonBackGroundColor" Color="Red"/>
                </ResourceDictionary>
                <ResourceDictionary x:Key="Dark">
                    <SolidColorBrush x:Key="CustomButtonBackGroundColor" Color="Blue"/>
                </ResourceDictionary>
            </ResourceDictionary.ThemeDictionaries>

            <Style x:Key="ExampleButtonStyle" TargetType="Button">
                <Setter Property="FontSize" Value="14"/>
                <Setter Property="FontWeight" Value="Normal"/>
                <Setter Property="Background" Value="{ThemeResource CustomButtonBackGroundColor}"/>
            </Style>
        </ResourceDictionary>
    </Page.Resources>
</Page>

然后你可以像这样添加你的按钮:

<Button Content="Example Button" Style="{StaticResource ExampleButtonStyle}"/>

【讨论】:

  • 如果我有 有没有办法只设置 BorderBrush ={ThemeResource CustomButtonBackGroundColor"} 这里 BorderBrush 是按钮样式的默认值。
  • 我面临的问题是在样式模板中, BorderBrush 指向 SystemControlBackgroundBaseLowBrush 。如果我将 BorderBrsuh 设置为 {ThemeResource CustomButtonBackGroundColor"},颜色不会改变,但如果我设置 SystemControlBackgroundBaseLowBrush,颜色会改变。但是,我无法更改 SystemControlBackgroundBaseLowBrush,因为这是在 generic.xaml 中定义的,并且也将被其他控件使用。
  • @novice_coder 如果颜色没有根据主题而改变,那么你仍然在某个地方做错了。您能否用我的答案以及您的代码现在的样子更新您的问题?
【解决方案2】:

#2 将是最佳解决方案,但不仅仅是 Red,还需要在 ResourceDictionary.ThemeDictionaries 的某处定义 {ThemeResource MyBrush} 和 MyBrush

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2018-02-07
    • 2015-10-04
    相关资源
    最近更新 更多