我的网格中有以下资源 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}}"
,因为这将确保我们将 Button 基于当前的 style/theme
。在这种情况下,它将是 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>
关于c# - 使用主题时更改按钮的默认背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13156199/