如何在app.xaml中设置默认的WPF窗口样式? [英] How to set default WPF Window Style in app.xaml?

查看:243
本文介绍了如何在app.xaml中设置默认的WPF窗口样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在app.xaml的WPF Windows应用程序中为每个窗口设置默认样式。到目前为止,我在app.xaml中具有以下内容:

I am trying to set the default Style for every window in my WPF Windows application in my app.xaml. So far i have this in app.xaml:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

运行应用程序时,我可以使窗口以这种样式显示(但不是VS设计器)通过专门告诉窗口通过以下方式使用此样式:

I can get the window to appear with this style when running the app (but not is VS designer) by specifically telling the window to use this style via:

Style="{DynamicResource WindowStyle}

这可行,但并不理想,所以我该怎么做:

This works, but is not ideal. So how do I:


  1. 是否所有窗口都自动使用该样式(因此不必在每个窗口上都指定样式)?

  2. VS设计器是否显示了样式?

谢谢!

推荐答案

要添加到雷说了什么:

对于样式,您需要提供键/ ID或指定TargetType。

For the Styles, you either need to supply a Key/ID or specify a TargetType.

如果FrameworkElement没有显式指定的
样式,它将
始终查找样式资源,
使用其自身的类型作为键

-WPF编程(出售,格里菲斯)

If a FrameworkElement does not have an explicitly specified Style, it will always look for a Style resource, using its own type as the key
- Programming WPF (Sells, Griffith)

如果您提供TargetType,该类型的所有实例都将应用样式。但是派生类型不会...似乎。 <样式TargetType = {x:Type Window}> 不适用于所有自定义派生/窗口。 <样式TargetType = {x:Type local:MyWindow}> 仅适用于MyWindow。因此,选项为

If you supply a TargetType, all instances of that type will have the style applied. However derived types will not... it seems. <Style TargetType="{x:Type Window}"> will not work for all your custom derivations/windows. <Style TargetType="{x:Type local:MyWindow}"> will apply to only MyWindow. So the options are


  • 使用您指定为每个窗口的Style属性的键控样式应用样式。设计器将显示样式化的窗口。

  • Use a Keyed Style that you specify as the Style property of every window you want to apply the style. The designer will show the styled window.

    <Application.Resources>
        <Style x:Key="MyWindowStyle">
            <Setter Property="Control.Background" Value="PaleGreen"/>
            <Setter Property="Window.Title" Value="Styled Window"/>
        </Style>
    </Application.Resources> ...
    <Window x:Class="MyNS.MyWindow" Style="{StaticResource MyWindowStyleKey}">  ...




  • 或者您可以从自定义BaseWindow类派生(具有自己的怪癖),您可以在Ctor / Initialization / Load阶段设置一次Style属性。然后,所有派生将自动应用样式。 但是设计人员不会注意到您的样式。您需要运行您的应用才能查看所应用的样式。。我猜设计师只是在运行InitializeComponent(这是自动/设计人员生成的代码)因此,应用了XAML,但没有应用自定义代码。

    • Or you could derive from a custom BaseWindow class (which has its own quirks), where you set the Style property during the Ctor/Initialization/Load stage once. All Derivations would then automatically have the style applied. But the designer won't take notice of your style You need to run your app to see the style being applied.. I'm guessing the designer just runs InitializeComponent (which is auto/designer generated code) so XAML is applied but not custom code-behind.
    • 所以我要说明确指定的样式是最不起作用的。无论如何,您都可以集中更改样式的各个方面。

      So I'd say explicitly specified styles are the least work. You can anyways change aspects of the Style centrally.

      这篇关于如何在app.xaml中设置默认的WPF窗口样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆