C# 在 WPF 中为整个应用程序设置外观的推荐方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/120914/
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-08-03 14:31:15  来源:igfitidea点击:

What is the recommended way to skin an entire application in WPF?

提问by Pop Catalin

I want my WPF application to be skinnable, by applying a certain XAML template, and the changes to be application wide, even for dynamic controls or controls that aren't even in the visual/logical tree.

我希望我的 WPF 应用程序可以通过应用某个 XAML 模板和更改为应用程序范围内的外观,即使对于动态控件或什至不在可视/逻辑树中的控件也是如此。

What can I use to accomplish this type of functionality? Are there any good resources or tutorials that show how this specific task can be done?

我可以用什么来完成这种类型的功能?是否有任何好的资源或教程可以展示如何完成此特定任务?

采纳答案by Maurice

The basic approach to take is using resources all through your application and dynamically replacing the resources at runtime.

采取的基本方法是在整个应用程序中使用资源并在运行时动态替换资源。

See http://www.nablasoft.com/alkampfer/index.php/2008/05/22/simple-skinnable-and-theme-management-in-wpf-user-interface/for the basic approach

有关基本方法,请参阅http://www.nablasoft.com/alkampfer/index.php/2008/05/22/simple-skinnable-and-theme-management-in-wpf-user-interface/

回答by rudigrobler

The replacing of resource will work but I found "structural skinning" to be more powerfull! Read more about it on CodeProject...

替换资源会起作用,但我发现“结构化皮肤”更强大!在 CodeProject 上阅读更多关于它的信息......

http://www.codeproject.com/KB/WPF/podder1.aspx

http://www.codeproject.com/KB/WPF/podder1.aspx

回答by Pop Catalin

I have found the way to apply generic templates to all controls without using template keys. The solution is to use the type of the control as the Style key.

我找到了将通用模板应用于所有控件而不使用模板键的方法。解决方法是使用控件的类型作为 Style 键。

Example:

例子:

 <Application.Resources>
    <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
        <Setter Property="Button.Background" Value="CornflowerBlue"/>
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate x:Name="MyTemplate">
                    ...
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

here the Style key is x:Key="{x:Type Button}", so the style will be applied to all controls of type button without the control declaring the Style property to be a static or dynamic resource.

这里的 Style 键是 x:Key="{x:Type Button}",因此该样式将应用于所有类型为 button 的控件,而无需控件将 Style 属性声明为静态或动态资源。