Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How do I discard all inherited styles?

Tags:

styles

wpf

How do I tell a control or block of controls to not inherit any styles and revert back to the defaults?

I need this because I have an overall theme that mostly works, but for one piece really screws up my design.

like image 810
Jonathan Allen Avatar asked Jan 26 '10 07:01

Jonathan Allen


2 Answers

You can create your own container for the piece of UI in question, and set InheritanceBehavior to SkipAllNow (or the value appropriate for your situation):

using System.Windows;
using System.Windows.Controls;

public class NoInheritanceContentControl : ContentControl
{
    public NoInheritanceContentControl()
    {
        InheritanceBehavior = InheritanceBehavior.SkipAllNow;
    }
}

Then you can just stick NoInheritanceContentControl in your visual tree and anything inside it won't inherit styles.

like image 87
Kent Boogaart Avatar answered Nov 06 '22 20:11

Kent Boogaart


This is tricky but you can try this,

  1. Create DefaultStyles resource dictionary
  2. Create named styles of the control you want to retain default style of as following..

    <Style 
        x:Key="DefaultButtonStyle" 
        TargetType="{x:Type Button}"  
        BasedOn="{StaticResource {x:Type Button}}"/>
    
  3. Make sure this DefaultStyles is added before any of your themes are applied, it should be the first entry in your app resources.

  4. Then you can apply style "DefaultButtonStyle" to any control at runtime to retain its default style.

like image 42
Akash Kava Avatar answered Nov 06 '22 22:11

Akash Kava