Change default background color of buttons while using a theme?

自古美人都是妖i 提交于 2019-12-12 11:20:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!