Another Way to Undo Implicit Styles

|

Several months ago, Mike Hillberg blogged about how to undo implicit styles, recently I come across a situation in which I apply my custom styles at the app level, and then I want one portion of controls in my app to take on the custom styles, and the other portion of controls to pick up the default styles(aka theme styles), and when I look back at Mike's solution, I find that the technique he suggests doesn't solve my problem, because it's really cumbersome and impractical to set the Style property to null on every control which I want to be decorated using my custom styles. the more I think about this issue, the more I think it's something impossible to do with current bit of WPF until Ian Griffiths points me to the InheritanceBehavior property which is exposed by FrameworkElement class, unfortunately this property is marked as protected, so we need to subclass it:

public class SystemThemeConnector : ContentControl
{
    public SystemThemeConnector() : base()
    {
        this.InheritanceBehavior = InheritanceBehavior.SkipToThemeNext;
    }
}

You can see that SystemThemeConnector is derived from ContentControl, and in the default constructor, I simply set the value of InheritanceBehavior property to InheritanceBehavior.SkipToThemeNext which means that any control which is below the SystemThemeConnector in the logical tree will directly look up the theme styles, bypassing the styles we apply at any level in the logical tree. So if you want any control to be styled using system theme, you can simply put it inside the SystemThemeConnector, and you are done:)

I guess that Microsoft Expression Interative Designer probably holds the similar approach here, we can see that although Expression Team has created an amazing look and feel for this product, any control put inside the design surface will pick up the default look and feel, that's the best application of this property:)

0 comments: