lepoco / lepoco/wpfui

Creating a new window object breaks the theme switcher

Open
#866 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
9.6k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

Creating a new window object in the MainWindow class makes the theme Switcher work incorrectly. The other design is only applied partially.
If the starting theme is the Dark Theme this means the background stays dark and some parts of the user controls get brighter but not all.
If the starting theme is the Light Theme this means the background stays in white and all user controls get white, essentially rendering them invisible.

To Reproduce
  1. Setup a new WPF Project
  2. Make the MainWindow class a type of FluentWindow
  3. Create a button and method to switch from dark theme to light and the other way :
if (Wpf.Ui.Appearance.ApplicationThemeManager.GetAppTheme() == Wpf.Ui.Appearance.ApplicationTheme.Dark)
{
    Wpf.Ui.Appearance.ApplicationThemeManager.Apply(
    Wpf.Ui.Appearance.ApplicationTheme.Light    // Theme type
    );
    

}
else if (Wpf.Ui.Appearance.ApplicationThemeManager.GetAppTheme() == Wpf.Ui.Appearance.ApplicationTheme.Light)
{
    Wpf.Ui.Appearance.ApplicationThemeManager.Apply(
    Wpf.Ui.Appearance.ApplicationTheme.Dark    // Theme type
    );
    
}
  1. create a new FluentWindow in the project named window1
  2. start the application and hit the button to change the theme. It should work as expected.
  3. create a object for this newly created windows directly inside the MainWindow class:
public partial class MainWindow : FluentWindow
{
    private Window1 _window = new();

    private void some_method(){
    }

}
  1. start the application and hit the button to change the theme. It shouldn't work as expected.
Expected behavior

A click on the button should apply the new theme on the windows and every user control correctly.

Screenshots

If i try to switch from Light to Dark theme:

switch_to_dark_theme

If i try to switch from Dark to Light theme:

switch_to_light_theme

OS version

Windows 10

.NET version

.Net 8.0

WPF-UI NuGet version

3.0.0-preview.12

Additional context

simply removing private Window1 _window = new(); fixes this behavior. If the window gets declared inside a method of the MainWindow class, the problem disappears too. But only for the MainWindow.
This code :

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            Window1 _window = new();
            _window.Show();


        }

will make MainWindow work as expected, but if the Theme is changed the new _window does now show the behavior shown in the screenshots above.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with MainWindow and Window1, comparing a field initializer (private Window1 _window = new();) with creating the window inside Button_Click. Start by tracing Wpf.Ui.Appearance.ApplicationThemeManager.Apply and the window initialization path; done means theme changes update MainWindow, the new window, and all user controls correctly in both directions.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.