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

标签 c# wpf themes

我的网格中有以下资源 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/

相关文章:

c# - 弹出窗口中的文本框未在 WPF 应用程序中获得焦点

c# - 如何为表单控件创建模板函数?

c# - 如何自定义ASP.Net Login控件的外观?

c# - 为什么 SourceUpdated 事件不触发我在 WPF 中的图像控件?

android - 带有动画更改的自定义进度对话框的大小

ios - 使用应用内设置和多个导航/选项卡栏和文本字段自定义背景更改 iOS 应用程序的主题

asp.net - 在 ASP.NET 中临时覆盖主题 CSS

c# - 两次 Socket.BeginReceive 调用之间的 TCP 数据包会发生什么情况?

c# - 新字符串类型

c# - 如何在 XAML 中创建类的实例?