locked
Overriding implict style breaks ThemeResource references?

    Question

  • In a Windows 8.1 store app, when I define an implicit style, say for a TextBox, the light/dark theme changes I make to built-in ThemeResources seem to be ignored.

    For instance,

    1. I've set up a ThemeResource file for the Default (Dark) theme, and set the color of TextBoxBackgroundThemeBrush to Lime. 
    2. I then copied the default TextBox template to two different resource dictionary files and made a change to the border color and width (for visual cues).  The files are identical except for defining an x:Key in the "explicit" theme file.
    3. The three XAML files (my theme resource dictionary and the two style files are added to App.xaml as MergedDictionaries.
    4. I added two textboxes to a vanilla page - one has the explicit style applied, one relies on the implicit style... the result is below:

    Note that the Implicit Style has lost the Lime background, even though it refers to {ThemeResource TextBoxBackgroundThemeBrush} just like the explicit style.


    Monday, February 09, 2015 3:56 PM

Answers

  • Interesting...  it's incredibly subtle that making the style implicit versus explicit changes the lookup sequence in such a dramatic way.

    It seems like the way around this is to include my theme dictionaries as a merged resource dictionary in the resource dictionary where my implicit styles are defined.  That seems redundant since everyother reference can find the resource just fine from the inclusion in App.xaml.


    • Marked as answer by Jim O'NeilMVP Friday, February 20, 2015 1:59 AM
    Friday, February 20, 2015 1:59 AM

All replies

  • Hi,

    How do you override the ThemeResource in Dark Them? Please share a reproduce sample. I create a project to  override the ThemeResource and apply Implicit Style to TextBox:

    Override ThemeResource by use of ResourceDictionary.ThemeDictionaries property in App.xaml:

     <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.ThemeDictionaries>
                    <ResourceDictionary x:Key="Default">
                        <SolidColorBrush x:Key="TextBoxBackgroundThemeBrush" Color="Red" />
                    </ResourceDictionary>
    
                </ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Assets/Dictionary1.xaml"></ResourceDictionary>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
       

    The Dictionary1.xaml below:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App5">
        <Style TargetType="TextBox">
            <Setter Property="BorderBrush" Value="Yellow"/>
        </Style>
       
    </ResourceDictionary>
    

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Tuesday, February 10, 2015 3:34 AM
  • Something similar, but I am completely overriding the ControlTemplate for the TextBox.  You'll find the repro here:  http://1drv.ms/1CQYFkc

    Thanks!

    Tuesday, February 10, 2015 6:40 PM
  • Hi Jim,

    Thanks for your repro, I can see the exactly behavior as you describe in your question.

    However I think the behavior could be explained by the Lookup behavior for XAML resource references, you may find more information from here: ResourceDictionary and XAML resource references.

    If a resource isn't found in the initial scope, the scope expands. The lookup behavior continues on throughout the locations and scopes that a XAML resource could possibly be defined by an app or by the system.

    When the TextBox is apply implicit style, the initial scope is system resource dictionary and there is the default setting for TextBoxBackgroundThemeBrush, then it take the default color. Again another small test, if we modify the implicit style by changing the resource key name, also we change the name in DarkTheme.xaml

    ImplicitTextBoxStyle.xaml:

        <Style TargetType="TextBox">
            <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
            <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
            <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}" />
            <Setter Property="Background" Value="{ThemeResource TextBoxBackgroundThemeBrush1}" />

    DarkTheme.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <SolidColorBrush x:Key="TextBoxBackgroundThemeBrush1" Color="Lime" />
            </ResourceDictionary>
            
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>

    The error will be thrown while we tried to debug it, looks like the Resource cannot be found. Probably the expend is outward expansion but not downward expansion.

    When the TextBox is apply explicit style, the initial scope is ResourceDictionary, and system will look into all the ResourceDictionaries to find the correct key, then it stop, Lime color will be displayed.

    Note  It is a common practice to define all the immediate resources at the root level of a page, both to take advantage of this resource-lookup behavior and also as a convention of XAML markup style.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, February 18, 2015 7:57 AM
    Moderator
  • Interesting...  it's incredibly subtle that making the style implicit versus explicit changes the lookup sequence in such a dramatic way.

    It seems like the way around this is to include my theme dictionaries as a merged resource dictionary in the resource dictionary where my implicit styles are defined.  That seems redundant since everyother reference can find the resource just fine from the inclusion in App.xaml.


    • Marked as answer by Jim O'NeilMVP Friday, February 20, 2015 1:59 AM
    Friday, February 20, 2015 1:59 AM