WPF - Fields reloaded when change to high contrast

Anderson Rodrigues Cavalcante 316 Reputation points
2023-09-29T11:49:04.27+00:00

I have this page in WPF .NET 5.0. When I change from OS current theme (light or dark) to High Contrast or from High Contrast to Light or Dark, the page is reloaded and the fields on the page are cleaned.
Is this a WPF limitation or a normal behavior when changing to High Contrast event?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,906 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
808 questions
{count} votes

Accepted answer
  1. Viorel 117.2K Reputation points
    2023-09-29T19:56:23.35+00:00

    According to documentation, the Loaded event might be re-raised as a result of system theme changes (and maybe for other reasons):

    To skip the initialisation of the fields, try a boolean member:

    
    bool loaded = false;
    
    void Page_Loaded( object sender, RoutedEventArgs e )
    {
        if( loaded ) return;
        loaded = true;
    
        // fill the fields...
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.