Home > Software design >  WPF XAML Windows 11 Accent Color
WPF XAML Windows 11 Accent Color

Time:11-05

Is there a simple way to get the Windows 10 / Windows 11 accent color in XAML?

I looked at many examples, but most of them do not work. I also saw some examples that mimic the color changing behavior in C#, but they do not work in Windows 11.

My code:

<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}"
    Color="{x:Static SystemParameters.WindowGlassBrush}"/>

I just want to set the Color to windows current accent color.

Thank you.

CodePudding user response:

You should be able to retrieve the accent colour using the UISettings.GetColorValue WinRT API.

Depending on whether you target .NET 5 or an earlier version, you should either set the TargetFramework to net5.0-windows10.0.* or install the Microsoft.Windows.SDK.Contracts NuGet package as described here.

You can then create a SolidColorBrush using the return value of the API:

var color = new Windows.UI.ViewManagement.UISettings()
    .GetColorValue(UIColorType.Accent);
var brush = new SolidColorBrush
    (Color.FromArgb(color.A, color.R, color.G, color.B));

CodePudding user response:

What worked in Windows 10, works also in Windows 11.

OS: Windows 11 Build 10.0.22000.282

VS: 2022 Preview 7.0

WPF: .NET 6

UserLabel.Background = SystemParameters.WindowGlassBrush;
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBase}"
       x:Key="TextColored">
    <Setter Property="FontWeight"
            Value="SemiBold"/>
    <Setter Property="Foreground"
            Value="{x:Static SystemParameters.WindowGlassBrush}"/>
</Style>

This worked without adding any NuGets.

  • Related