Controls derived from native controls aren't themed

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 3 years ago by IneedHelp
Version: 16.1.0632
Platform: .NET 4.0
Environment: Windows 7 (64-bit)
Avatar

Not sure whether this is a known issue or not, but apparently custom controls that derive from native controls aren't themed, even if the theme is applied on a specific control or if it's enabled application-wide.

For example, if I create a class "ResetButton : Button", this control will not be themed and can not be themed by using the ThemeManager.

Comments (3)

Answer - Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

When you enable native control theming, we effectively inject some implicit Styles that target native controls into the app's Resources.  Those implicit styles might not get applied if your other derived controls have their own default Style.

That being said, it's easy to make implicit Styles for those controls too if styles aren't working:

<Style TargetType="local:ResetButton" BasedOn="{StaticResource {x:Static themes:SharedResourceKeys.ButtonBaseStyleKey}}" />

Or if you do define a default Style for derived controls, then just use the BasedOn above too.  There are SharedResourceKeys entries for all the native controls we theme.


Actipro Software Support

Posted 3 years ago by IneedHelp
Avatar

Would you please show me the equivallent of that in C# code?

Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Normally it would be done something like this:

var style = new Style();
style.TargetType = typeof(ResetButton);
style.BasedOn = this.FindResource(ActiproSoftware.Windows.Themes.SharedResourceKeys.ButtonBaseStyleKey) as Style;
// Application.Current.Resources.Add(null, style);

But I'm not sure how to add an implicit Style to a ResourceDictionary via code-behind since the last commented-out line doesn't work.  Implicit styles don't use x:Key.  There doesn't seem to be any documentation we can find that tells how either.  We'd recommend using XAML for this anyhow, per above... there's no issue in pure XAML.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 13 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.