WPF overriding default style on standard controls at the theme level

103 views Asked by At

I want to override the default style of the Button control in wpf, by simply referencing my "styles" assembly. I don't want to use merge dictionaries or an implicit style. What I want to do is to override the default theme style of the 'Button' control in my Styles project Themes/generic.xaml file and have that style be used as the default style for the button. That way once the styles project assembly is loaded the referencing application will use the button style defined in the styles project as the default style for the entire application.

I believe this is possible, because this is the behavior I see when using the DevExpress Theming. All that I have to do is reference the theme assembly (for me this is the DevExpress.Wpf.Themes.Office2019Colorful assembly), and add in one of their controls to any page in the app or child user control (I assume this is what causes the assembly to load). Once those steps are done even the standard WPF buttons are styled for the theme, with no additional steps. Furthermore if you have a standard button defined in the app with an implicit style defined, the implicit style will take effect without overriding the style properties applying the theming to the standard button. This would not be the case if DevExpress was simply adding an implicit style targeting the standard button to the app resources.

What I have tried: Defining a button style in Themes/generic.xaml like so. (using a simple control template so I can see if it takes effect)

    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Then in NewButton.cs I am attempting to override the default style key for the button. Because DefaultStyleKeyProperty is protected I figured inheritence is the only way to set it, but I think I am missing something, by not actually setting the button's DefaultStyleKeyProperty .

    public class NewButton : Button
    {
        static NewButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Button), new FrameworkPropertyMetadata(typeof(Button)));
        }
    }

I have looked through DevExpress's source code, but I either missed it or don't understand how they are achieving this. Please let me know if you have any idea of how to achieve this or know how it is accomplished in DevExpress.

0

There are 0 answers