This documentation is for the HandyControls
(unofficial version) If you are looking for the HandyControl
(official version) documentation, read here
All classes related to Theme Located in the HandyControl.Themes namespace.
1 | Using HandyControl.Themes; |
Name | Type | Remarks |
---|---|---|
ActualApplicationThemeChanged | Event | This event is called whenever the ApplicationTheme changes |
SystemThemeChanged | Event | This event is called whenever the Windows Theme changes |
AccentColor | Property | |
ActualApplicationTheme | Property | |
ApplicationTheme | Property | |
UsingSystemTheme | Property | |
GetActualTheme | Method | |
GetHasThemeResources | Method | |
GetRequestedAccentColor | Method | |
GetRequestedTheme | Method | |
GetSystemTheme | Method | |
SetHasThemeResources | Method | |
SetRequestedAccentColor | Method | |
SetRequestedTheme | Method | |
AddActualThemeChangedHandler | Method | |
RemoveActualThemeChangedHandler | Method | |
GetElementTheme | Method | |
GetAccentColorFromSystem | Method |
How to Address Resource?
1 | <ResourceDictionary> |
Set the application theme
To override the default settings, set properties on the ThemeResources
class in App.xaml. For example:
Xaml
1 | <hc:ThemeResources RequestedTheme="Dark" AccentColor="Red"/> |
Values set this way are applied at both design time and runtime. If you’d like to change them at runtime, it’s usually more convenient to use the ThemeManager
class, which supports data binding. For example:
Code-Behind
1 | ThemeManager.Current.ApplicationTheme = ApplicationTheme.Dark; |
- If you want to specify the Accent Color for whole application in
Xaml (ThemeResources)
, you must use theAccentColor
property. - If you want to specify the Accent Color for whole application in
C# code
, you must useThemeManager
Class andAccentColor
property. - If you want to specify the Accent Color of the
FrameworkElements
, you must useThemeManager
Class andRequestedAccentColor
attached property. - If you want to specify the ApplicationTheme of the
FrameworkElements
, you must useThemeManager
Class andRequestedTheme
attached property.
Name | NameSpace | Type | Remarks |
---|---|---|---|
AccentColor | ThemeResource | Brush | specify the Accent Color for whole application |
AccentColor | ThemeManager | DependencyProperty | specify the Accent Color for whole application |
RequestedAccentColor | ThemeManager | AttachedProperty | specify the Accent Color for FrameworkElements |
RequestedTheme | ThemeManager | AttachedProperty | specify the ApplicationTheme for FrameworkElements |
If you want to set the AccentColor to the default Color You must assign a null
value to it.
1 | ThemeManager.Current.AccentColor= null; |
Override the theme at element level
Set the ThemeManager.RequestedTheme
or hc:ThemeManager.RequestedAccentColor
attached property on any FrameworkElement. For example:
1 | <Border Background="{DynamicResource RegionBrush}" Padding="12" hc:ThemeManager.RequestedTheme="Dark"> |
Now this Border and its descendants will use the dark theme while the theme used by the rest of the app remains unchanged.
This feature sometimes doesn’t work correctly at design time.
Change Application Theme with System Theme Change
If UsingSystemTheme
is enabled in Xaml (ThemeResources)
or C# code (ThemeManager)
(default is false
), By changing the Windows theme (Light/Dark and Accent Color), the theme of the program also changes automatically.
For systems running Windows 10, the system accent color and system Theme is used. for windows 7 system accent color and the light theme is used.
For systems running an earlier version of Windows, the light theme and the color PrimaryBrush (default blue) are used.
Xaml
1 | <hc:ThemeResources UsingSystemTheme="true"/> |
Code-Behind
1 | ThemeManager.Current.UsingSystemTheme = true; |
SystemThemeChanged Event
There is also an event called SystemThemeChanged
if you want to do more.
Xaml
1 | <hc:ThemeResources UsingSystemTheme="true" SystemThemeChanged="ThemeResources_OnSystemThemeChanged"/> |
1 | private void ThemeResources_OnSystemThemeChanged(object? sender, FunctionEventArgs<ThemeManager.SystemTheme> e) |
Code-Behind
if you want to use this event in another class you can use ThemeManager
1 | ThemeManager.Current.SystemThemeChanged += OnSystemThemeChanged; |
Change Application Theme with System (App) Theme Change
If UsingSystemAppTheme
is enabled in Xaml (ThemeResources)
or C# code (ThemeManager)
(default is false
), By changing the Windows App theme (Light/Dark), the theme of the program also changes automatically.
Xaml
1 | <hc:ThemeResources UsingSystemAppTheme="true"/> |
ThemeDictionaries (Override predefined colors)
You can overwrite predefined resources in ThemeDictionaries, Note that you need to know the resource key to override it.
also you can change your colors and styles according to the theme of the program.
Edit the hc:ThemeResources
element in App.xaml
to following:
1 | <hc:ThemeResources> |
ColorPresetResources (Customization)
See here To customize all the colors and brushes
Enable IntelliSense for XAML resources
See here
Theme Changing Animation
When you are changing the theme, call the AnimateTheme
before and after the Theme Changing:
1 | ThemeAnimationHelper.AnimateTheme(HandyControlDemo.MainWindow.Instance, ThemeAnimationHelper.SlideDirection.Top, 0.3, 1, 0.5); |