是否在使用主题时更改按钮的默认背景颜色? [英] Change default background color of buttons while using a theme?

查看:56
本文介绍了是否在使用主题时更改按钮的默认背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网格中有以下资源XML:

<Grid.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Classic;component/themes/classic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Grid.Resources>

这是有效的,我加载了经典主题。但是经典的主题按钮背景是非常白的吗?是否可以更改此主题中按钮的默认背景颜色?

推荐答案

您可以使用样式仅将背景颜色设置为其他颜色。

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
      <Setter Property="Background" Value="Green" />
</Style>
这里的关键是使用BasedOn="{StaticResource {x:Type Button}}",因为这将确保按钮基于当前的style/theme。在本例中,这将是Classic。如果我们没有赋值,它将简单地将其基于原始主题,即Aero


我通常有一个XAML文件,我在其中存储所有自定义主题数据并加载它

<ResourceDictionary Source="ThemesMyTheme.xaml"/>

在这种情况下,它将包含以下内容

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008">

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Green" />
    </Style>

</ResourceDictionary>

这篇关于是否在使用主题时更改按钮的默认背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆