title: WPF project DarkMode and LightMode problem
body: <p>I have Wpf C#.net project.</p>
  <p>I wanted to make to do list project and I have problem.</p>
  <p>in my project I use registerykey to theme names and then I retrive them and use them in my project but I have a serious problem and my problem is when I clicked on the button , the button theme will change but the main window won't change.</p>
  <p>Please help me to fix this problem.
  This the peace of code that I used to create and set and retrieve registry key :</p>
  <pre><code>using Microsoft.Win32;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  namespace HiPlanner.Classes
  {
      public class General_Info_Class01
      {
          public double SysMonSizeWidth = SystemParameters.PrimaryScreenWidth;
          public double SysMonSizeHeight = SystemParameters.PrimaryScreenHeight;
  
          public byte ThemeRegisteryNum()
          {
              RegistryKey ThemeStatusRegKey = Registry.CurrentUser.OpenSubKey(@&quot;SoftWare/HiPlanner&quot;);
              Byte Result = byte.Parse(ThemeStatusRegKey.GetValue(&quot;ThemeNumber&quot;).ToString());
              ThemeStatusRegKey.Close();
              return Result;
          }
  
          public void SetThemeRegNumBtnMethod(byte ThemeNo)
          {
              RegistryKey ThemeRegKey = Registry.CurrentUser.OpenSubKey(@&quot;SoftWare/HiPlanner&quot;,true);
              ThemeRegKey.SetValue(&quot;ThemeNumber&quot;, ThemeNo);
              ThemeRegKey.Close();
          }
  
      }
  }
  </code></pre>
  <p>this is the user control i created as a dark and light theme button :</p>
  <p>design Code :</p>
  <pre><code>&lt;UserControl x:Class=&quot;HiPlanner.DesignFiles.UserControls.ThemeStatusButton&quot;
               xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
               xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
               xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
               xmlns:local=&quot;clr-namespace:HiPlanner.DesignFiles.UserControls&quot;
               mc:Ignorable=&quot;d&quot;
               d:DesignHeight=&quot;450&quot; d:DesignWidth=&quot;800&quot;&gt;
      &lt;Grid x:Name=&quot;TSBTNMainGrid&quot; Style=&quot;{StaticResource TSBTNGridStyle}&quot;&gt;
          &lt;Button x:Name=&quot;ThemeStatusBTN&quot; Style=&quot;{StaticResource StatusBTN-WhiteMode}&quot; Click=&quot;ThemeStatusBTN_Click&quot;&gt;
              &lt;Border x:Name=&quot;StatusCircleBorder&quot; Style=&quot;{StaticResource StatusBorder-WhiteMode}&quot;&gt;
                  &lt;Border.RenderTransform&gt;
                      &lt;TranslateTransform/&gt;
                  &lt;/Border.RenderTransform&gt;
                  &lt;Image x:Name=&quot;ThemeStatusBtnImg&quot; Style=&quot;{StaticResource StatusBtnImgStyle-WhiteMode}&quot;/&gt;
              &lt;/Border&gt;
          &lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  
  design dictionary :
  
  &lt;ResourceDictionary xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
                      xmlns:local=&quot;clr-namespace:HiPlanner.DesignFiles.DesignDictionaries&quot;&gt;
  
      &lt;!--Start Status Button Style--&gt;
  
  
      &lt;Style TargetType=&quot;Grid&quot; x:Key=&quot;TSBTNGridStyle&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;110&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;60&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;Transparent&quot;/&gt;
          &lt;Setter Property=&quot;RenderOptions.BitmapScalingMode&quot; Value=&quot;HighQuality&quot;/&gt;
      &lt;/Style&gt;
      
      &lt;Style TargetType=&quot;Button&quot; x:Key=&quot;StatusBTN-WhiteMode&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;100&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;40&quot;/&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;#E6E6E6&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;BorderBrush&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;LinearGradientBrush StartPoint=&quot;0,0.5&quot; EndPoint=&quot;1,0.5&quot;&gt;
                      &lt;GradientStop Color=&quot;#005DDE&quot; Offset=&quot;0&quot;/&gt;
                      &lt;GradientStop Color=&quot;#00A8DE&quot; Offset=&quot;1&quot;/&gt;
                  &lt;/LinearGradientBrush&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;2&quot;/&gt;
          &lt;Setter Property=&quot;Template&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                      &lt;Border Background=&quot;{TemplateBinding Background}&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot; BorderThickness=&quot;{TemplateBinding BorderThickness}&quot; CornerRadius=&quot;20&quot;&gt;
                          &lt;ContentPresenter VerticalAlignment=&quot;Center&quot; HorizontalAlignment=&quot;Center&quot;/&gt;
                      &lt;/Border&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType=&quot;Button&quot; x:Key=&quot;StatusBTN-DarkMode&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;100&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;40&quot;/&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;#1A1D24&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;VerticalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalContentAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;BorderBrush&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;LinearGradientBrush StartPoint=&quot;0,0.5&quot; EndPoint=&quot;1,0.5&quot;&gt;
                      &lt;GradientStop Color=&quot;#005DDE&quot; Offset=&quot;0&quot;/&gt;
                      &lt;GradientStop Color=&quot;#00A8DE&quot; Offset=&quot;1&quot;/&gt;
                  &lt;/LinearGradientBrush&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;2&quot;/&gt;
          &lt;Setter Property=&quot;Template&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                      &lt;Border Background=&quot;{TemplateBinding Background}&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot; BorderThickness=&quot;{TemplateBinding BorderThickness}&quot; CornerRadius=&quot;20&quot;&gt;
                          &lt;ContentPresenter VerticalAlignment=&quot;Center&quot; HorizontalAlignment=&quot;Center&quot;/&gt;
                      &lt;/Border&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
  
      &lt;Style TargetType=&quot;Border&quot; x:Key=&quot;StatusBorder-WhiteMode&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;30&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;30&quot;/&gt;
          &lt;Setter Property=&quot;BorderBrush&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;LinearGradientBrush StartPoint=&quot;0,0.5&quot; EndPoint=&quot;1,0.5&quot;&gt;
                      &lt;GradientStop Color=&quot;#005DDE&quot; Offset=&quot;0&quot;/&gt;
                      &lt;GradientStop Color=&quot;#00A8DE&quot; Offset=&quot;1&quot;/&gt;
                  &lt;/LinearGradientBrush&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;2&quot;/&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;#E3E3E3&quot;/&gt;
          &lt;Setter Property=&quot;CornerRadius&quot; Value=&quot;30&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;Effect&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;DropShadowEffect Color=&quot;#D1D1D1&quot; BlurRadius=&quot;10&quot; Direction=&quot;360&quot; Opacity=&quot;1&quot; RenderingBias=&quot;Quality&quot; RenderOptions.BitmapScalingMode=&quot;HighQuality&quot; ShadowDepth=&quot;0&quot;/&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;Margin&quot; Value=&quot;50,0,0,0&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType=&quot;Border&quot; x:Key=&quot;StatusBorder-DarkMode&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;30&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;30&quot;/&gt;
          &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;#002136&quot;/&gt;
          &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;2&quot;/&gt;
          &lt;Setter Property=&quot;Background&quot; Value=&quot;#002A45&quot;/&gt;
          &lt;Setter Property=&quot;CornerRadius&quot; Value=&quot;30&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Center&quot;/&gt;
          &lt;Setter Property=&quot;Effect&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;DropShadowEffect Color=&quot;#002136&quot; BlurRadius=&quot;10&quot; Direction=&quot;360&quot; Opacity=&quot;1&quot; RenderingBias=&quot;Quality&quot; RenderOptions.BitmapScalingMode=&quot;HighQuality&quot; ShadowDepth=&quot;0&quot;/&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property=&quot;Margin&quot; Value=&quot;0,0,50,0&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType=&quot;Image&quot; x:Key=&quot;StatusBtnImgStyle-WhiteMode&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;Auto&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;Auto&quot;/&gt;
          &lt;Setter Property=&quot;Stretch&quot; Value=&quot;UniformToFill&quot;/&gt;
          &lt;Setter Property=&quot;RenderOptions.BitmapScalingMode&quot; Value=&quot;HighQuality&quot;/&gt;
          &lt;Setter Property=&quot;Source&quot; Value=&quot;/HiPlanner;component/DesignFiles/Photos/StatusBtn-WhiteMode.png&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
          &lt;Setter Property=&quot;Margin&quot; Value=&quot;1&quot;/&gt;
      &lt;/Style&gt;
  
      &lt;Style TargetType=&quot;Image&quot; x:Key=&quot;StatusBtnImgStyle-DarkMode&quot;&gt;
          &lt;Setter Property=&quot;Width&quot; Value=&quot;Auto&quot;/&gt;
          &lt;Setter Property=&quot;Height&quot; Value=&quot;Auto&quot;/&gt;
          &lt;Setter Property=&quot;Stretch&quot; Value=&quot;UniformToFill&quot;/&gt;
          &lt;Setter Property=&quot;RenderOptions.BitmapScalingMode&quot; Value=&quot;HighQuality&quot;/&gt;
          &lt;Setter Property=&quot;Source&quot; Value=&quot;/HiPlanner;component/DesignFiles/Photos/StatusBtn-DarkMode.png&quot;/&gt;
          &lt;Setter Property=&quot;VerticalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
          &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
      &lt;/Style&gt;
      &lt;!--END Status Button Style--&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  <p>the c# code behind of button :</p>
  <pre><code>using Microsoft.Win32;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace HiPlanner.DesignFiles.UserControls
  {
      /// &lt;summary&gt;
      /// Interaction logic for ThemeStatusButton.xaml
      /// &lt;/summary&gt;
      public partial class ThemeStatusButton : UserControl
      {
          Classes.General_Info_Class01 GeneralInfo = new Classes.General_Info_Class01();
          Style ThemeStatusBtnStyleWhiteMode = (Style)Application.Current.FindResource(&quot;StatusBTN-WhiteMode&quot;);
          Style ThemeStatusBtnStyleDarkMode = (Style)Application.Current.FindResource(&quot;StatusBTN-DarkMode&quot;);
          Style ThemeStatusBorderStyleWhiteMode = (Style)Application.Current.FindResource(&quot;StatusBorder-WhiteMode&quot;);
          Style ThemeStatusBorderStyleDarkMode = (Style)Application.Current.FindResource(&quot;StatusBorder-DarkMode&quot;);
          Style ThemeStatusImgStyleWhiteMode = (Style)Application.Current.FindResource(&quot;StatusBtnImgStyle-WhiteMode&quot;);
          Style ThemeStatusImgStyleDarkMode = (Style)Application.Current.FindResource(&quot;StatusBtnImgStyle-DarkMode&quot;);
  
  
          public ThemeStatusButton()
          {
              InitializeComponent();
              SetDefaultThemeBTnTheme();
          }
  
          private void SetDefaultThemeBTnTheme()
          {
              if (GeneralInfo.ThemeRegisteryNum() == 0)
              {
                  ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
                  StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
                  ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
              }
              else if (GeneralInfo.ThemeRegisteryNum() == 1)
              {
                  ThemeStatusBTN.Style = ThemeStatusBtnStyleDarkMode;
                  StatusCircleBorder.Style = ThemeStatusBorderStyleDarkMode;
                  ThemeStatusBtnImg.Style = ThemeStatusImgStyleDarkMode;
              }
              else
              {
                  ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
                  StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
                  ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
              }
          }
  
          private void ThemeStatusBTN_Click(object sender, RoutedEventArgs e)
          {
  
              if (GeneralInfo.ThemeRegisteryNum() == 0)
              {
                  GeneralInfo.SetThemeRegNumBtnMethod(1);
                  ThemeStatusBTN.Style = ThemeStatusBtnStyleDarkMode;
                  StatusCircleBorder.Style = ThemeStatusBorderStyleDarkMode;
                  ThemeStatusBtnImg.Style = ThemeStatusImgStyleDarkMode;
  
  &lt;-- problem is here --&gt;
  
                  RunMainWindowThemeChecker();
  
  &lt;-- problem is here --&gt;
  
              }
              else
              {
                  GeneralInfo.SetThemeRegNumBtnMethod(0);
                  ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
                  StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
                  ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
                  RunMainWindowThemeChecker();
              }
          }
  &lt;-- problem is here --&gt;
          private void RunMainWindowThemeChecker()
          {
              MainWindow MyMainWindow = new MainWindow(0);
              MyMainWindow.SetWindowTheme();
          }
  
  &lt;-- problem is here --&gt;
  
      }
  </code></pre>
  <p>the main window code behind:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  using HiPlanner;
  
  
  namespace HiPlanner
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          Classes.General_Info_Class01 GeneralInfo = new Classes.General_Info_Class01();
  
          public MainWindow()
          {
              InitializeComponent();
              UserResizeWindow();
              SetWindowTheme();
          }
  
          public MainWindow(byte none)
          {
              InitializeComponent();
          }
  
  
          private void UserResizeWindow()
          {
  
              double UserSysMonSizeWidth = GeneralInfo.SysMonSizeWidth;
              double UserSysMonSizeHeight = GeneralInfo.SysMonSizeHeight;
              Style FullHDStyle = (Style)Application.Current.FindResource(&quot;FullHDWindowStyle&quot;);
              Style HDStyle = (Style)Application.Current.FindResource(&quot;HDWindowStyle&quot;);
  
              if (UserSysMonSizeWidth &gt;= 1920 &amp;&amp; UserSysMonSizeHeight &gt;= 1080)
              {
                  MainWindow01.Style = FullHDStyle;
              }
              else
              {
                  MainWindow01.Style = HDStyle;
              }
  
          }
  &lt;-- problem is here --&gt;
          public void SetWindowTheme()
          {
  
              Classes.General_Info_Class01 SecGeneralinfo = new Classes.General_Info_Class01();
              Style WhiteModeTheme = (Style)Application.Current.FindResource(&quot;MainBorderStyle-WhiteMode&quot;);
              Style DarkModeTheme = (Style)Application.Current.FindResource(&quot;MainBorderStyle-DarkMode&quot;);
              Style WhiteModeLogoIcon = (Style)Application.Current.FindResource(&quot;LogoBorderStyle-WhiteMode&quot;);
              Style DarkModeLogoIcon = (Style)Application.Current.FindResource(&quot;LogoBorderStyle-DarkMode&quot;);
              Style WhiteModeLogoText = (Style)Application.Current.FindResource(&quot;LogoTextLBL&quot;);
              Style DarkModeLogoText = (Style)Application.Current.FindResource(&quot;LogoTextLBL-DarkMode&quot;);
  
              if (SecGeneralinfo.ThemeRegisteryNum() == 0)
              {
                  MainWindowBorder.Style = WhiteModeTheme;
                  LogoBorder.Style = WhiteModeLogoIcon;
                  LogoTextLabel.Style = WhiteModeLogoText;
              }
              else if (SecGeneralinfo.ThemeRegisteryNum() == 1)
              {
                  MainWindowBorder.Style = DarkModeTheme;
                  LogoBorder.Style = DarkModeLogoIcon;
                  LogoTextLabel.Style = DarkModeLogoText;
              }
              else
              {
                  MainWindowBorder.Style = WhiteModeTheme;
                  LogoBorder.Style = WhiteModeLogoIcon;
                  LogoTextLabel.Style = WhiteModeLogoText;
              }
  
          }
  
          private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              this.DragMove();
          }
  
          private void Testbtn_Click(object sender, RoutedEventArgs e)
          {
              Windows.AddPlanWin Addplan = new Windows.AddPlanWin();
              Addplan.ShowDialog();
          }
  
  &lt;-- problem is here --&gt;
      }
  }
  </code></pre>
  <p>I know its long but please help me.</p>
  <p>As a matter of fact I want to know how can I change main  window theme on real time like the button?</p>
  <p>Best regards to anyone help me.</p>