Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Style in WPF XAML?

Tags:

c#

styles

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?

Thanks

Working Solution

Switching between themes in WPF

like image 510
Sandeep Bansal Avatar asked Aug 04 '11 17:08

Sandeep Bansal


1 Answers

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() {     if(Application.Current.Resources.Contains(typeof(TextBox)))         Application.Current.Resources.Remove(typeof(TextBox));      Application.Current.Resources.Add(typeof(TextBox),                                             new Style() { TargetType = typeof(TextBox) }); }  public void SetCustomStyle() {     if (Application.Current.Resources.Contains(typeof(TextBox)))         Application.Current.Resources.Remove(typeof(TextBox));      Application.Current.Resources.Add(typeof(TextBox),                                        myStyle); } 
like image 75
loxxy Avatar answered Sep 21 '22 03:09

loxxy