我想通过定义明暗模式的样式来修改我的按钮的颜色。我有一个定义如下的样式:
<Style x:Key="ExampleButton" TargetType="Button">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontWeight" Value="Normal" />
</Style>
在另一个文件中,我在浅色模式下将 ButtonBackground 定义为“红色”。我的期望是当我的应用程序使用这个 ExampleButton 时,它应该有一个红色的背景。但是,我仍然可以使用其默认的 BorderBackground(即 SystemControlBackgroundBaseLowBrush)看到此按钮。我可以通过 3 种方式解决这个问题:
将 SystemControlBackgroundBaseLowBrush 重新定义为“Red”,这可行,但我可能将 SystemControlBackgroundBaseLowBrush 用于其他控件,因此不想使用这种方法。
在上面的代码中添加另一个 setter 并有这样的东西
<Style x:Key="ExampleButton" TargetType="Button">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="Background" Value="Red"/>
</Style>
编辑按钮模板中的值
哪个是实现这一目标的最优雅的解决方案?除了我上面描述的 3 个之外,还有更好的解决方案吗?
我希望避免添加整个模板来修改颜色
最好的方法确实是SolidColorBrush
根据应用程序主题定义不同的。首先,您应该ResourceDictionary.ThemeDictionaries
在您的Application.Resources
或Page.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}"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句