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
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With