how update WebView2 control in C# WPF application as per Color Theme selected in visual studio 2022 like Light, Dark etc

Ajay Gera 20 Reputation points
2024-08-19T17:46:32.5333333+00:00

how update WebView2 control in C# WPF application as per Color Theme selected in visual studio 2022.

Color theme can be changed in visual studio 2022 from Tools -> Options -> Color Theme:

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,059 questions
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,858 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Bruce (SqlWork.com) 64,486 Reputation points
    2024-08-19T21:15:20.1966667+00:00

    the visual studio color theme is only for visual studio's controls, not the controls in the application project. if you want your application to support color themes, then you add code to support themes. if you want trigger off the windows theme see:

    https://learn.microsoft.com/en-us/answers/questions/715081/how-to-detect-windows-dark-mode


  3. Hongrui Yu-MSFT 1,605 Reputation points Microsoft Vendor
    2024-08-20T07:13:54.8866667+00:00

    Hi,@Ajay Gera. Welcome to Microsoft Q&A. 

    If you want to apply the theme color of Visual Studio to WebView2, the process can be broken down into the following two steps:

    1.Get the theme color of Visual Studio

    You can refer to the following document to get the theme color of Visual Studio.

    intellisense - How to get current used color theme of Visual Studio - Stack Overflow

     

    2.In the listening event in step 1, you get the theme color of Visual Studio. You could set the theme of WebView2 in the listening event through conditional judgment.

    You could set the theme color in WebView2 through CoreWebView2Profile.PreferredColorScheme

    Reference example

    <wv2:WebView2 Name="webView" Source="https://www.microsoft.com"/>
    
    
    webView.CoreWebView2.Profile.PreferredColorScheme = Microsoft.Web.WebView2.Core.CoreWebView2PreferredColorScheme.Dark;
    
    

    Related Documents:

    CoreWebView2Profile.PreferredColorScheme Property (Microsoft.Web.WebView2.Core) | Microsoft Learn


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.