34

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

4 Answers 4

81

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.


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);
}
16
  • How about if I were to change the style for all TextBoxes, and then be able to change it back to the styles used before again? Commented Aug 4, 2011 at 18:14
  • I don't think that suits what I need. atm I have one style which is targeted for all textboxes. I would like to be able to revert every textbox in the program away from the custom style I set and make it null so it can be back to the Windows Default. And also have C# code to change it back to my custom style. any possibility? Thanks Commented Aug 4, 2011 at 18:26
  • Is this something that you are doing for testing your app in default style or is it for implementing a 'switch to default style' feature in your app? If it is testing, then simply set the null style in XAML & remove it later.
    – loxxy
    Commented Aug 4, 2011 at 18:36
  • It is a way to switch the styles in my application to the default and back to the custom if the user wishes. So I need a switch on this end Commented Aug 4, 2011 at 18:48
  • Thanks I get an error when using the SetDefault: Item has already been added. Key in dictionary: 'System.Windows.Controls.TextBox' Key being added: 'System.Windows.Controls.TextBox' Commented Aug 4, 2011 at 19:30
23

You could inject a blank Style that would take precedence over your other Style. Like so:

<Window>
    <Window.Resources>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="Red" />
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <Style TargetType="TextBox" />
        </Grid.Resources>
    </Grid>
</Window>

In the example above, only the Grid's implicit Style would be applied to TextBoxes in the Grid. You could even add this to the Grid programmatically, something like:

this.grid.Resources.Add(typeof(TextBox), new Style() { TargetType = typeof(TextBox) });
0
3

I know the answer has been accepted, but i want to add my solution which works awesome in the following scenario:

  • One main application using mahapps.metro
  • additional project imported from the main application with no reference to mahapps.metro, it is imported as a plugin (loading compiled .dll on the fly)
  • using the < ToolBar> re-styles everything to null therefore the mahapps.metro styles are not being applied to items inside the toolbar.
  • usercontrol is used to provide custom controls to the main application.

in the user control root set the resources:

<UserControl.Resources>
    <Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" />
    <Style x:Key="ComboBoxStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}" />
</UserControl.Resources>

then the toolbar code can be the following

                <ToolBar>
                    Block Template:
                    <ComboBox Style="{StaticResource ComboBoxStyle}"/>
                    <Button Content="Generate!" Style="{StaticResource ButtonStyle}"/>
                </ToolBar>

this successfully applies the main application style to the controls inside the < ToolBar>

1

In Xaml, you can override this by setting a style explicitly. In code-behind, you can also set the style explicitly.

<TextBox Style="{StaticResource SomeOtherStyle}"/>

myTextBox.Style = Application.Resources["SomeOtherStyle"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.