如何在 WPF 中为 GUI 创建皮肤?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18037445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:21:44  来源:igfitidea点击:

how to create skins for a GUI in WPF?

c#wpfxamlskins

提问by James Joshua Street

I was wondering how I should go about creating custom settings for all the color schemes and such. I know I can create styles for individual components or parts..but how should I create a set of skins?

我想知道我应该如何为所有配色方案等创建自定义设置。我知道我可以为单个组件或部件创建样式..但是我应该如何创建一组皮肤?

for example, right now I'm using a maroon colored gradientbrush in a lot of subcontrols. However, I'm sure that people other than me will hate the color scheme.

例如,现在我在很多子控件中使用栗色渐变画笔。但是,我相信除了我之外的人都会讨厌这种配色方案。

I know that I can create a dependency property on my top level control for the color and then bind the individual parts that need that color to that dependency property. However, there will need to be many properties. Should I just create a separate style object that contains all these properties and place it as field in my user control?

我知道我可以在我的顶级颜色控件上创建一个依赖属性,然后将需要该颜色的各个部分绑定到该依赖属性。但是,需要有很多属性。我是否应该创建一个包含所有这些属性的单独样式对象并将其作为字段放置在我的用户控件中?

I'm just wondering if there are other ways of doing this in WPF. For example, I guess there could be some way of doing this in xaml or utilizing some built in class in the default libraries.

我只是想知道在 WPF 中是否还有其他方法可以做到这一点。例如,我想可能有某种方法可以在 xaml 中执行此操作或利用默认库中的某些内置类。

采纳答案by kmatyaszek

You can do this by creating new resource dictionary and define there colors and control templates for your controls.

您可以通过创建新的资源字典并为您的控件定义颜色和控件模板来做到这一点。

Example you can find in WPF Themesproject (download link).

您可以在WPF Themes项目(下载链接)中找到示例。

You can change your style by changing resource dictionary, e.g:

您可以通过更改资源字典来更改您的样式,例如:

<Application x:Class="ThemesSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary Source="ExpressionDark.xaml"/>
    </Application.Resources>
</Application>

If you want to change theme at runtime you should use following code:

如果您想在运行时更改主题,您应该使用以下代码:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("BureauBlack.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(dict);