WPF Style Inheritance Behavior

Recently I was using a window style with MahAppsMetro, now it applies the styles at application level. One of the things I am doing is embedding AvalonEdit in one such window. Now with Metro styling it applies styles to the controls and in my case SearchPanel in AvalonEdit was styled too! This is fine, but the results were quite un-expected:

Metro Styling applied to SearchPanel

Metro Styling applied to SearchPanel


Now, of course I do want to keep Metro Style for my application, however want to keep AvalonEdit styling intact as well.

Thinking further the simplest thing to do would be to be to somehow disable inheritance of styles at some level. I found a post on stackoverflow discussing this. However for me it was not to stop inheriting styles but to fallback to theme. Documentation on this topic is pretty scarce.

Now to get things to work, I wrapped the AvalonEdit.TextEditor in a custom Control which would InheritanceBehavior.SkipToThemeNow.

and xaml was modified to use this NoInheritanceContentControl

1
2
3
4
5
6
    <local:NoInheritanceContentControl Grid.Column="2" >
            <ed:TextEditor Name="_txtEd" IsReadOnly="True" ShowLineNumbers="True" 
                           FontFamily="Consolas" 
                           FontSize="10pt"/>
    </local:NoInheritanceContentControl>
        

and the code for the same is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class NoInheritanceContentControl : ContentControl
    {
        //  https://stackoverflow.com/a/2138114
        //  but here skip to theme instead of skipping all
        //  otherwise the SearchPanel is rendered completely translucent
        //
        public NoInheritanceContentControl()
        {
            InheritanceBehavior = InheritanceBehavior.SkipToThemeNow;
        }
    }


With following changes, I get the SearchPanel to show up correctly:

/images/2018-10-26_avalonedit2.png

InheritanceBehavior


InheritanceBehavior has following members and I was curious how each of them turn out:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    //
    // Summary:
    //     Indicates the current mode of lookup for both property value inheritance, a resource
    //     lookup, and a RelativeSource FindAncestor lookup.
    public enum InheritanceBehavior
    {
        //
        // Summary:
        //     Property value inheritance lookup will query the current element and continue
        //     walking up the element tree to the page root.
        Default = 0,
        //
        // Summary:
        //     Property value inheritance lookup will not query the current element or any further.
        SkipToAppNow = 1,
        //
        // Summary:
        //     Property value inheritance lookup will query the current element but not any
        //     further.
        SkipToAppNext = 2,
        //
        // Summary:
        //     Property value inheritance lookup will not query the current element or any further.
        SkipToThemeNow = 3,
        //
        // Summary:
        //     Property value inheritance lookup will query the current element but not any
        //     further.
        SkipToThemeNext = 4,
        //
        // Summary:
        //     Property value inheritance lookup will not query the current element or any further.
        SkipAllNow = 5,
        //
        // Summary:
        //     Property value inheritance lookup will query the current element but not any
        //     further.
        SkipAllNext = 6
    }

 

Default

/images/2018-10-26_avalonedit_ibdefault.png

 

SkipToAppNow

/images/2018-10-26_avalonedit_ibdefault.png

 

SkipToAppNext

/images/2018-10-26_avalonedit_ibdefault.png

 

SkipToThemeNow

/images/2018-10-26_avalonedit2.png

 

SkipToThemeNext

/images/2018-10-26_avalonedit2.png

 

SkipAllNow

/images/2018-10-26_avalonedit_ibskipallnow.png

 

SkipAllNext

/images/2018-10-26_avalonedit_ibskipallnow.png