Search code examples
wpfstylesribbon

Reset Style in certain portion of Visual Tree in WPF


I am setting some global styles for TextBox, ComboBox, TextBlock, etc. in my App.xaml file. I want these styles to flow down through the visual tree for a consistent look and that is what they are doing.

However, I am using a Ribbon in part of my UI. The style customizations are completely throwing off the appearance of the Ribbon. The ribbon sits at the same level as many other UI elements so is there a way to just reset the style for the ribbon and its visual children


Solution

  • I don't think that you can change this wpf behaviour.

    But you can try to contain all global styles to specific resource dictionary and use it in all other VisualTree elements.

    Or you can try something like this:

    <Window ...>
        <Window.Resources>
            <Style TargetType="Button">
                <Setter Property="Background" Value="Red" />
            </Style>
        </Window.Resources>
    
        <DockPanel LastChildFill="True">
            <Button x:Name="button1" Content="button" DockPanel.Dock="Top" />
            <StackPanel>
                <StackPanel.Resources>
                    <Style TargetType="Button">
                    </Style>
                </StackPanel.Resources>
                <Button x:Name="button2" Content="lala" />
            </StackPanel>
        </DockPanel>
    </Window>
    

    Element with name "button2" will be default.