Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add style for Title of ContentPage in .Net MAUI?

I'm working with .NET MAUI to create a desktop app.

Here, I have a ContentPage with the title My title.

<ContentPage
    x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="My title">
....

Its title is aligned left by default, do you know how to align it to the center of the page? If possible, I also want to add more style to it too. enter image description here

Edit: suggested solution

  • I forget to mention that my page is considered NavigationPage. So, here is my solution:
<ContentPage
    x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>
 <NavigationPage.TitleView>
        <Label
            x:Name="pageTitleView"
            HorizontalTextAlignment="Center"
            Text="My title"/>
</NavigationPage.TitleView>
......

In code-behind xaml.cs file:

 protected override void OnSizeAllocated(double width, double height)
 {
     base.OnSizeAllocated(width, height);
     pageTitleView.WidthRequest = width;
 }
  • If you're working with Shell app, you could follow the answer from @mm8
like image 290
Hoàng Trần Avatar asked Nov 02 '25 11:11

Hoàng Trần


1 Answers

You could define your own TitleView:

<ContentPage                          x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="Planification">
    <Shell.TitleView>
        <Grid>
            <Label Text="My Title" HorizontalOptions="Center" VerticalOptions="Center" />
        </Grid>
    </Shell.TitleView>
    ...
like image 148
mm8 Avatar answered Nov 04 '25 03:11

mm8