How can I change the Windows title bar to a specific color like purple?

Eduardo Gomez 3,426 Reputation points
2023-12-05T23:04:41.56+00:00

I have a purple title bar (Primary color)

User's image

How can I achieve the same on windows?

User's image

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,412 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,491 Reputation points Microsoft Vendor
    2023-12-06T05:52:40.0666667+00:00

    Hello,

    If you use windows 11 and .NET 7, you can custom this titlebar's color by TitleBar's BackgroundColor property in the MauiPrograms.cs

    As note:Color customization is ignored when the app runs on Windows 10.

      builder
                    .UseMauiApp<App>()
                   
                    .ConfigureLifecycleEvents(events =>
                    {
    #if WINDOWS
                        events.AddWindows(windowsLifecycleBuilder =>
                        {
                            windowsLifecycleBuilder.OnWindowCreated(window =>
                            {
                                var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
                                var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
                                var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);                           
                                var titleBar = appWindow.TitleBar;
                               titleBar.ForegroundColor = Colors.White.ToWindowsColor();                 
                                titleBar.BackgroundColor = Color.FromRgb(81, 43, 212).ToWindowsColor();
                                titleBar.ButtonBackgroundColor = Color.FromRgb(81, 43, 212).ToWindowsColor();                       
                            });
    #endif
                    });
            })
    

    However, if you use .NET 8, titleBar.BackgroundColor is not working, but other ButtonBackgroundColor is working as normal. Please report this issue in MAUI Github repo and share link here.

    Here is another solution to change the titlebar's background color, it this working for .NET 8.

    Please open the App.xaml in the Platforms\Windows folder, add following code in the <maui:MauiWinUIApplication> tag, you can change the title bar's background color by Background in Border control, and you need to reset the title by TextBlock control

     <maui:MauiWinUIApplication.Resources>
            <DataTemplate x:Key="MauiAppTitleBarTemplate">
                <Grid >
                    <Border
                      Canvas.ZIndex="1"  
                      VerticalAlignment="Stretch"
                      Background="Pink"
                      Margin="0,0,0,0">
                        <TextBlock  
                          Foreground="Red"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center"
                          Text="your application title"  
                          FontWeight="Bold"/>
                    </Border>
                </Grid>
            </DataTemplate>
        </maui:MauiWinUIApplication.Resources>
    

    Best Regards,

    Leon Lu


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.