Application.Resources 与 Window.Resources 中的隐式样式? [英] Implicit styles in Application.Resources vs Window.Resources?

查看:40
本文介绍了Application.Resources 与 Window.Resources 中的隐式样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 这个问题 并注意到在 TextBlock 中放置了一个隐式的 TextBlock 样式code>Application.Resources 将该样式应用于所有 TextBlocks,甚至那些位于其他控件中的文本块,例如 ButtonsComboBoxes

I was looking at this question and noticed that placing an implicit TextBlock style in Application.Resources applies that style to all TextBlocks, even those inside other controls such as Buttons, ComboBoxes, etc

<Application.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</Application.Resources>

Window.Resources 中放置隐式样式 不跨越控件模板边界,所以ButtonsComboBoxes 之类的东西保持它们默认的黑色文本.

Placing the implicit style in Window.Resources does not cross control template boundaries, so things like Buttons and ComboBoxes maintain their default black text.

<Window.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</Window.Resources>

此外,在 Application.Resources 中添加默认样式可以使您无法使用其他隐式样式覆盖该样式.

Furthermore, adding the default style in the Application.Resources makes it so you can't overwrite that style with another implicit style.

<!-- Doesn't work if implicit style with same property is in Application.Resources -->
<ComboBox.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</ComboBox.Resources>

我的问题是:

  • 这是为什么?

  • Why is this?

Application.ResourcesWindows.Resources 之间还有其他区别吗?

Are there other differences between Application.Resources and Windows.Resources?

什么时候应该使用一个?

When should use one over the other?

我知道 Application.Resources 适用于整个应用程序,而 Window.Resources 仅适用于窗口,但是我想知道为什么 中的样式>Application 的处理方式与 Window

I understand that Application.Resources apply to the entire application, while Window.Resources apply to the window only, however I want to know why the styles in Application are treated differently than styles in Window

推荐答案

这确实是添加到 WPF 中的唯一特殊处理,它是由设计完成的.实现它的代码可以在 FrameworkElement 中的 FindImplicitStyleResource 方法中找到,它有效地:

This is really the only special handling added to WPF and it was done by design. The code that implements it can be found in FrameworkElement, in the method FindImplicitStyleResource, which effectively does:

internal static object FindImplicitStyleResource(FrameworkElement fe, object resourceKey, out object source)
{
        // ...
        DependencyObject boundaryElement = null;
        if (!(fe is Control))
            boundaryElement = fe.TemplatedParent;
        // ...
}

所以经验法则是隐式样式总是应用于控件(即派生自 Control).假设可以找到隐式样式.

So the rule of thumb is that implicit Styles are always applied to controls (i.e. derives from Control). Assuming the implicit Style can be found.

对于 ControlTemplate 中使用的不是从 Control 派生的元素,例如 TextBlock,隐式样式查找不会跨越它的模板化的父级.在上面的例子中,这将是 ComboBox.

For elements used in a ControlTemplate that do not derive from Control, such as TextBlock, the implicit Style look up will not cross it's templated parent. In your case above, this would be the ComboBox.

我相信这样做是为了使 TextBlock 的非应用程序隐式样式不会无意中应用于控件模板中使用的 TextBlock 元素,开发人员可能知道也可能不知道在那里.隐式样式只会应用于开发人员在他们自己的 XAML 中实际创建的 TextBlock.

I believe this was done so that non-application implicit Styles for TextBlock were not inadvertently applied to TextBlock elements used in control templates, which the developer may or may not have known were there. The implicit Styles would only be applied to TextBlocks actually created by the developer in their own XAML.

应用程序隐式样式仍将允许全局样式,例如增加字体大小.但可能引起了更多的混乱.

The application implicit Styles would still allow global styling, such as increasing font size. But has probably caused more confusion than it's worth.

没有很好的答案来说明何时使用一种与另一种,因为它们各有其功能.显然,如果您不想影响应用程序中的每个 TextBlock,则不应将样式放在应用程序资源中.

There is no good answer to say when to use one versus the other, as they each have their function. Obviously if you don't want to affect every TextBlock in your application, you shouldn't put the style in the application resources.

但请记住,这会影响任何非 Control 元素,例如 Shape 元素.

But keep in mind that this affects any non-Control element, such as Shape elements.

这篇关于Application.Resources 与 Window.Resources 中的隐式样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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