Wpf Project Dark and Light Mode Doesn't work

  • Thread starter Thread starter IKING251
  • Start date Start date
I

IKING251

Guest
hello Everyone

i have Wpf C#.net project.

i wanted to make to do list project and i have problem.

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 click on the button , the button theme will change but the main window won't change

please help me to fix this problem.



this the peace of code that i use to create and set and retrive registery key :



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(@"SoftWare/HiPlanner");
Byte Result = byte.Parse(ThemeStatusRegKey.GetValue("ThemeNumber").ToString());
ThemeStatusRegKey.Close();
return Result;
}

public void SetThemeRegNumBtnMethod(byte ThemeNo)
{
RegistryKey ThemeRegKey = Registry.CurrentUser.OpenSubKey(@"SoftWare/HiPlanner",true);
ThemeRegKey.SetValue("ThemeNumber", ThemeNo);
ThemeRegKey.Close();
}

}
}



this is the user control i created as a dark and light theme button :



design Code :

<UserControl x:Class="HiPlanner.DesignFiles.UserControls.ThemeStatusButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:HiPlanner.DesignFiles.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="TSBTNMainGrid" Style="{StaticResource TSBTNGridStyle}">
<Button x:Name="ThemeStatusBTN" Style="{StaticResource StatusBTN-WhiteMode}" Click="ThemeStatusBTN_Click">
<Border x:Name="StatusCircleBorder" Style="{StaticResource StatusBorder-WhiteMode}">
<Border.RenderTransform>
<TranslateTransform/>
</Border.RenderTransform>
<Image x:Name="ThemeStatusBtnImg" Style="{StaticResource StatusBtnImgStyle-WhiteMode}"/>
</Border>
</Button>
</Grid>
</UserControl>

design dictionary :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HiPlanner.DesignFiles.DesignDictionaries">

<!--Start Status Button Style-->


<Style TargetType="Grid" x:Key="TSBTNGridStyle">
<Setter Property="Width" Value="110"/>
<Setter Property="Height" Value="60"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/>
</Style>

<Style TargetType="Button" x:Key="StatusBTN-WhiteMode">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Background" Value="#E6E6E6"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="#005DDE" Offset="0"/>
<GradientStop Color="#00A8DE" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style TargetType="Button" x:Key="StatusBTN-DarkMode">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Background" Value="#1A1D24"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="#005DDE" Offset="0"/>
<GradientStop Color="#00A8DE" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


<Style TargetType="Border" x:Key="StatusBorder-WhiteMode">
<Setter Property="Width" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="BorderBrush">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="#005DDE" Offset="0"/>
<GradientStop Color="#00A8DE" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="#E3E3E3"/>
<Setter Property="CornerRadius" Value="30"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Color="#D1D1D1" BlurRadius="10" Direction="360" Opacity="1" RenderingBias="Quality" RenderOptions.BitmapScalingMode="HighQuality" ShadowDepth="0"/>
</Setter.Value>
</Setter>
<Setter Property="Margin" Value="50,0,0,0"/>
</Style>

<Style TargetType="Border" x:Key="StatusBorder-DarkMode">
<Setter Property="Width" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="BorderBrush" Value="#002136"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="#002A45"/>
<Setter Property="CornerRadius" Value="30"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Color="#002136" BlurRadius="10" Direction="360" Opacity="1" RenderingBias="Quality" RenderOptions.BitmapScalingMode="HighQuality" ShadowDepth="0"/>
</Setter.Value>
</Setter>
<Setter Property="Margin" Value="0,0,50,0"/>
</Style>

<Style TargetType="Image" x:Key="StatusBtnImgStyle-WhiteMode">
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Stretch" Value="UniformToFill"/>
<Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/>
<Setter Property="Source" Value="/HiPlanner;component/DesignFiles/Photos/StatusBtn-WhiteMode.png"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Margin" Value="1"/>
</Style>

<Style TargetType="Image" x:Key="StatusBtnImgStyle-DarkMode">
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="Auto"/>
<Setter Property="Stretch" Value="UniformToFill"/>
<Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality"/>
<Setter Property="Source" Value="/HiPlanner;component/DesignFiles/Photos/StatusBtn-DarkMode.png"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
<!--END Status Button Style-->
</ResourceDictionary>

the c# code behind of button :

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
{
/// <summary>
/// Interaction logic for ThemeStatusButton.xaml
/// </summary>
public partial class ThemeStatusButton : UserControl
{
Classes.General_Info_Class01 GeneralInfo = new Classes.General_Info_Class01();
Style ThemeStatusBtnStyleWhiteMode = (Style)Application.Current.FindResource("StatusBTN-WhiteMode");
Style ThemeStatusBtnStyleDarkMode = (Style)Application.Current.FindResource("StatusBTN-DarkMode");
Style ThemeStatusBorderStyleWhiteMode = (Style)Application.Current.FindResource("StatusBorder-WhiteMode");
Style ThemeStatusBorderStyleDarkMode = (Style)Application.Current.FindResource("StatusBorder-DarkMode");
Style ThemeStatusImgStyleWhiteMode = (Style)Application.Current.FindResource("StatusBtnImgStyle-WhiteMode");
Style ThemeStatusImgStyleDarkMode = (Style)Application.Current.FindResource("StatusBtnImgStyle-DarkMode");


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;

<-- problem is here -->

RunMainWindowThemeChecker();


<-- problem is here -->

}
else
{
GeneralInfo.SetThemeRegNumBtnMethod(0);
ThemeStatusBTN.Style = ThemeStatusBtnStyleWhiteMode;
StatusCircleBorder.Style = ThemeStatusBorderStyleWhiteMode;
ThemeStatusBtnImg.Style = ThemeStatusImgStyleWhiteMode;
RunMainWindowThemeChecker();
}
}
<-- problem is here -->
private void RunMainWindowThemeChecker()
{
MainWindow MyMainWindow = new MainWindow(0);
MyMainWindow.SetWindowTheme();
}

<-- problem is here -->

}


the main window code behind:


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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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("FullHDWindowStyle");
Style HDStyle = (Style)Application.Current.FindResource("HDWindowStyle");

if (UserSysMonSizeWidth >= 1920 && UserSysMonSizeHeight >= 1080)
{
MainWindow01.Style = FullHDStyle;
}
else
{
MainWindow01.Style = HDStyle;
}

}
<-- problem is here -->
public void SetWindowTheme()
{

Classes.General_Info_Class01 SecGeneralinfo = new Classes.General_Info_Class01();
Style WhiteModeTheme = (Style)Application.Current.FindResource("MainBorderStyle-WhiteMode");
Style DarkModeTheme = (Style)Application.Current.FindResource("MainBorderStyle-DarkMode");
Style WhiteModeLogoIcon = (Style)Application.Current.FindResource("LogoBorderStyle-WhiteMode");
Style DarkModeLogoIcon = (Style)Application.Current.FindResource("LogoBorderStyle-DarkMode");
Style WhiteModeLogoText = (Style)Application.Current.FindResource("LogoTextLBL");
Style DarkModeLogoText = (Style)Application.Current.FindResource("LogoTextLBL-DarkMode");

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();
}

<-- problem is here -->
}
}

i know its long but please help me.

as a matter of fact i want to know how can i change main window theme on real time like the button.

best regards to anyone help me.

Continue reading...
 
Back
Top