Hello @Eduardo Gomez ,
The essence of a custom title bar is to cover the top bar of the window client area to the original non-client area (title bar, menu bar, or window frame), so if your new window wants to use a custom title bar, you must have multiple rows of grids, which means that each new window must use Grid.RowDefinitions
.
<Window
x:Class="App1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="App1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</Window>
Another point is that you don't need to be responsible for the maximize and minimize buttons. The following is my test code, I hope it will be helpful to you.
UserControl
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="App1.CustomTitleBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Interactivity="using:Microsoft.VisualBasic"
mc:Ignorable="d">
<Grid x:Name="AppTitleBar"
Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="LeftPaddingColumn" Width="0"/>
<ColumnDefinition x:Name="IconColumn" Width="Auto"/>
<ColumnDefinition x:Name="TitleColumn" Width="Auto"/>
</Grid.ColumnDefinitions>
<Image x:Name="TitleBarIcon"
Source="ms-appx:///Assets/AppIcon.ico"
Grid.Column="1"
Width="16" Height="16"
Margin="8,0,4,0"/>
<TextBlock x:Name="TitleBarTextBlock"
Text="App title"
Style="{StaticResource CaptionTextBlockStyle}"
Grid.Column="2"
VerticalAlignment="Center">
</TextBlock>
</Grid>
</UserControl>
public sealed partial class CustomTitleBar : UserControl
{
public CustomTitleBar()
{
this.InitializeComponent();
}
}
App.xaml.cs
public partial class App : Application
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
ConfigureCustomTitleBar(m_window,"Test");
}
private Window? m_window;
public static void ConfigureCustomTitleBar(
Window window,
string title)
{
Debug.WriteLine("ConfigureCustomTitleBar is called!");
if (window is not null)
{
var customTitleBar = new CustomTitleBar();
window.ExtendsContentIntoTitleBar = true;
window.Title = title;
var windowHandle = WindowNative.GetWindowHandle(window);
var windowId = Win32Interop.GetWindowIdFromWindow(windowHandle);
var appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
// Attach title bar dynamically
var rootGrid = (Grid)window.Content;
if (rootGrid is not null)
{
rootGrid.Children.Insert(0, customTitleBar); // Add title bar at the top
}
}
}
}
Thank you.