Sandeep Bansal Sandeep Bansal - 1 year ago 234
C# Question

Disable Style in WPF XAML?

Is there anyway to turn off a style programatically?

As an example, I have a style that is linked to all textboxes

<Style TargetType="{x:Type TextBox}">

I would like to add some code to actually stop the style elements being used, so basically reverting back to the default control style.

I need a way to make a switch in my styles, so I can switch between Windows default style and my custom style through C# code.

Is there anyway to do this?


Working Solution

Switching between themes in WPF

Answer Source

For setting the style to default,

In XAMl use,

<TextBox Style="{x:Null}" />

In C# use,

myTextBox.Style = null;

If style needs to be set as null for multiple resources, see CodeNaked's response.

I feel, all the additional info should be in your question and not in the comments. Anyways, In code Behind I think this is what you are trying to achieve:

Style myStyle = (Style)Application.Current.Resources["myStyleName"];

public void SetDefaultStyle()

                                      new Style() { TargetType = typeof(TextBox) });

public void SetCustomStyle()
    if (Application.Current.Resources.Contains(typeof(TextBox)))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download