问题
I have the following resources XML in my grid:
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Classic;component/themes/classic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
And this works, I load in the classic theme. But the classic theme button backgrounds are very white? Is there any way I can change the default background color of buttons in this theme?
回答1:
You could use styles to set only the background color to something different.
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Background" Value="Green" />
</Style>
The key here is to use BasedOn="{StaticResource {x:Type Button}}"
, as this will make sure that we base the Button on the current style/theme
. In this case that would be Classic
. If we assign no value, it will simply base it on the original theme, which would be Aero
.
I normally have an XAML file where I store all my custom theme data and load it
<ResourceDictionary Source="Themes\MyTheme.xaml"/>
In this case it would contain the following
<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>
来源:https://stackoverflow.com/questions/13156199/change-default-background-color-of-buttons-while-using-a-theme