Home > OS >  Style Triggers Cause Control To Lose Theme
Style Triggers Cause Control To Lose Theme

Time:03-12

I have a WPF application using MahApps Metro for it's UI theming. I also need to use style triggers so I can appropriately determine whether a control is visible based on a property. The triggers work, but have the side effect of removing the theme. So now it looks like a default WPF unthemed CheckBox. Is there any way to preserve the MahApps Metro theme on the CheckBox?

Image of the XAML code for CheckBox, the declaration is:

CodePudding user response:

When you assign a style, you overwrite the default style or any style applied before. If you want to extend a style, you have specify the base style using the BasedOn property.

Styles can be based on other styles through this property. When you use this property, the new style will inherit the values of the original style that are not explicitly redefined in the new style.

  • Specify the control type to base your style on the implicit style of the control.

    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
       <!-- ...your setters and triggers. -->
    </Style>
    
  • Specify the key of the style that you want to base your style on.

    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MyCheckBoxBaseStyle}">
       <!-- ...your setters and triggers. -->
    </Style>
    

    The named styles for CheckBox in MahApps can be found here on GitHub.

Please be aware that although your Visibility triggers should work, other triggers that are already defined in the control template of the CheckBox styles take precedence and you will not be able to redefine them in your own style. If you ever hit that case, you have to copy the corresponding style from GitHub into your project and adapt it to your requirements.

  • Related