在使用主题时更改按钮的默认背景颜色?

4

我在我的表格中有以下资源XML:

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

这个可以实现,我加载了经典主题。但是经典主题的按钮背景非常白?有没有办法可以更改此主题中按钮的默认背景颜色?

1个回答

2
您可以使用样式将背景颜色设置为其他颜色。
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
      <Setter Property="Background" Value="Green" />
</Style>

重点在于使用BasedOn="{StaticResource {x:Type Button}}",这将确保我们基于当前的样式/主题来创建按钮。在本例中,它将是Classic。如果我们不指定值,它将简单地基于原始主题,即Aero
我通常会有一个XAML文件,用于存储所有自定义主题数据并加载它们。
<ResourceDictionary Source="Themes\MyTheme.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>

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