【WPF】WPF设置自定义皮肤主题

在 WPF 中,自定义皮肤主题通常是通过 ResourceDictionary 来实现的。资源字典可以包含控件样式、颜色、字体和其他 UI 元素的定义。为了创建一个自定义皮肤主题,你可以按照以下步骤进行:

1. 创建资源字典(ResourceDictionary)

首先,你需要创建一个包含主题样式的资源字典文件,例如 Theme.xaml

Theme.xaml 示例:
xml 复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- 定义主题颜色 -->
    <Color x:Key="PrimaryColor">#FF1A73E8</Color>
    <Color x:Key="SecondaryColor">#FF4CAF50</Color>

    <!-- 定义控件样式 -->
    <Style TargetType="Button">
        <Setter Property="Background" Value="{DynamicResource PrimaryColor}" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="16" />
    </Style>

    <Style TargetType="Window">
        <Setter Property="Background" Value="{DynamicResource SecondaryColor}" />
    </Style>
</ResourceDictionary>

2. 在 App.xaml 中引用主题

然后,在你的 App.xaml 中引用刚才创建的主题资源字典,以便整个应用程序使用该主题。

App.xaml 示例:
xml 复制代码
<Application x:Class="YourApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- 引用自定义主题 -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Theme.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

3. 使用动态资源

你可以在 WPF 控件中使用动态资源(DynamicResource)来引用资源字典中的元素,这样当资源发生变化时,UI 会自动更新。例如:

xml 复制代码
<Button Content="Click Me" Background="{DynamicResource PrimaryColor}" />

4. 实现主题切换(可选)

如果你希望能够动态切换主题,可以在 App.xaml.cs 中动态加载不同的资源字典。例如:

App.xaml.cs 示例:
csharp 复制代码
public partial class App : Application
{
    public void ChangeTheme(string themeName)
    {
        var uri = new Uri($"{themeName}.xaml", UriKind.Relative);
        var resourceDictionary = new ResourceDictionary { Source = uri };

        // 清空当前的资源字典并应用新的主题
        this.Resources.MergedDictionaries.Clear();
        this.Resources.MergedDictionaries.Add(resourceDictionary);
    }
}

然后,你可以通过代码调用 ChangeTheme 方法来切换主题:

csharp 复制代码
// 切换到新的主题
var app = (App)Application.Current;
app.ChangeTheme("DarkTheme");

5. 自定义控件样式

除了设置按钮和窗口的样式,你还可以为其他控件(如 TextBoxListBoxComboBox 等)定义样式。自定义控件样式和模板可以让你更灵活地控制皮肤的外观。

例如,定义 TextBox 的样式:

xml 复制代码
<Style TargetType="TextBox">
    <Setter Property="Background" Value="{DynamicResource PrimaryColor}" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="BorderBrush" Value="Gray" />
    <Setter Property="BorderThickness" Value="2" />
</Style>

这样,你就可以通过 ResourceDictionary 来定义和切换 WPF 应用的皮肤和主题。

相关推荐
2601_962174171 天前
Redis 简介
数据库·redis·wpf
SamChan903 天前
Python+PyMuPDF提取PDF表格并翻译:表格结构检测与双语重建实战
windows·python·ai·pdf·wpf
SamChan903 天前
Python+OpenCV检测PDF翻译后排版偏移:像素级格式一致性验证
python·opencv·ai·pdf·wpf
FuckPatience3 天前
WPF 拿到控件的最初样式,修改获得焦点样式
wpf
主宰者3 天前
【WPF】MainWindow前添加加载窗口,发现加载过程中有白色无内容窗口闪烁一下的解决方案
wpf
Vae_Mars4 天前
WPF中的ControlTemplate(控件模板)
wpf
SamChan904 天前
Python+ReportLab自动生成PDF翻译质量审计报告:从数据到可视化的完整方案
开发语言·python·ai·pdf·wpf
circuitsosk4 天前
多智能体协同在能源数据分析中的实践:分配-执行-校验闭环架构设计
python·数据分析·wpf·能源·并行计算·多智能体系统·agent协作
weixin199701080165 天前
《大促护航:电商API限流与降级,双11峰值500万调用的架构复盘》(附Python源码)
python·架构·wpf
hh9505 天前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区