Implicit styles in Application.Resources vs Window.Resources?

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;
        // ...
}

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.

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.

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.

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.

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

Leave a Comment