Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window does not apply implicit Window styles #10461

Open
dotMorten opened this issue Feb 14, 2025 · 2 comments
Open

Window does not apply implicit Window styles #10461

dotMorten opened this issue Feb 14, 2025 · 2 comments
Assignees

Comments

@dotMorten
Copy link
Contributor

Description

If I define an implicit style for a Window in App.xaml.cs, this style will never get applied. You're forced to explicitly set the style on every single window instance.

Reproduction Steps

  1. Create a new WPF application.
  2. Add the following style to App.xaml.cs:
    <Application.Resources>

        <Style TargetType="Window" x:Key="DefaultWindowStyle">
            <Setter Property="Background" Value="Red" />
        </Style>

        <Style BasedOn="{StaticResource DefaultWindowStyle}" TargetType="Window" />

    </Application.Resources>
  1. Run the application and notice the Window doesn't have a red Background.
  2. In MainWindow.xaml set the Window style explicitly by adding Style="{StaticResource DefaultWindowStyle}"
  3. Notice the window now has a red background.

Expected behavior

Implicit styles should also be applied to Window instances.

Actual behavior

Implicit styles do not apply to Window instances.

Regression?

No response

Known Workarounds

No known workaround for implicit styles - only explicit styles work.

Impact

Harder to maintain consistent look and feel of windows, or to create styling libraries that doesn't require extra code on every single window instance.

Configuration

.NET 9.0.101
Windows 11 23H2, x64

Other information

No response

@dipeshmsft
Copy link
Member

@dotMorten, we got into this but later found out that this was by design.

Implicit styles are applied to the exact type as defined in the TargetType of the style. They are not applied to derived classes.

Now, as MainWindow is subclassed from Window, therefore the implicit styles don't get applied to MainWindow.

@dotMorten
Copy link
Contributor Author

That’s not quite true though. If you create a style for ButtonBase it is also applied to both Button and ToggleButton sub classes. Why is Window any different?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants