question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inheriting the implicit style not working fine in WinUI 3 for desktop

See original GitHub issue

I have a custom control CustomControl1

public class CustomControl1 : Control
{
    public CustomControl1()
    {
        this.DefaultStyleKey = typeof(CustomControl1);
    }
}

Whose implicit style is

    <Style TargetType="local:CustomControl1">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CustomControl1">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And a second custom control CustomControl2, inheriting CustomControl1

public class CustomControl2 : CustomControl1
{
    public CustomControl2()
    {
        DefaultStyleKey = typeof(CustomControl2);
    }
}

whose implicit style only sets the Background

    <Style TargetType="local:CustomControl2">
        <Setter Property="Background" Value="Red"/>
    </Style>

The problem is the “Template” of CustomControl1 is not inherited automatically. Therefore nothing is rendered.

This looks as a bug as the implicit style of CustomControl2 doesn’t override Template, so it should take the parent one.

In case that were intentional, there should be at least a way to base the style on the parent control implicit style, I couldn’t find anything, x:Type doesn’t exist in WinUI. Is there any way round?

Here the repo project App12.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
arivoircommented, Feb 25, 2022

This is still a big issue. Is there any light in the horizon?

1reaction
roxkcommented, May 18, 2021

@arivoir Given the current constraint, I think you best bet is to wrap the control instead of inheriting it. If you need those properties/methods, then you will need to use the adapter pattern to adapt one by one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

XAML styles - Windows apps
In this article · WinUI and styles · Style basics · Apply an implicit or explicit style · Use based-on styles · Use...
Read more >
How to inherit from, or override elements of, the Default ...
I am trying to learn how to use Styles most effectively in WinUI 3 (from WindowsAppSDK 1.1.1) but I'm having difficulty getting simple ......
Read more >
Custom Style dynamic Inheritance of theme in UI for WPF
Hi, Telerik! I have created some helper class for setting default theme. In it I applied StyleManager.ApplicationTheme with needed default theme ...
Read more >
Implicit and Explicit Styles in XAML - Nick's .NET Travels
By explicitly setting the Style attribute on the Button, the implicit Style is no longer being applied. Luckily, we can create derived styles ......
Read more >
Application crashes if appearance properties are set from a ...
I have 2 issues to report. ... a StaticResource and DataGridView's style does not apply appearance properties ... And the resource file:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found