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

查看:37
本文介绍了如何在 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 in 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 设计师是否展示了风格?

谢谢!

推荐答案

补充一下 Ray 所说的:

To add on to what Ray says:

对于样式,您需要提供密钥/ID 或指定目标类型.

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

如果 FrameworkElement 没有明确指定的样式,它将总是寻找风格资源,使用自己的类型作为键
- 编程 WPF (Sells, Griffith)

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,则该类型的所有实例都将应用该样式.但是派生类型不会......似乎.<Style TargetType="{x:Type Window}"> 不适用于您的所有自定义派生/窗口.