mauiwinuititleview

Centering NavigationPage.TitleView in WinUI


Running my ContentPage in WinUI, which is wrapped with a NavigationPage at runtime, needs a custom TitleView

I've put the same content in both TitleView and Content sections:

<NavigationPage.TitleView>
    <Grid VerticalOptions="FillAndExpand" BackgroundColor="CornflowerBlue">
        <Label Text="Center me!" Style="{StaticResource H1}" TextColor="White" HorizontalOptions="Center"/>
    </Grid>
</NavigationPage.TitleView>

<ContentPage.Content>
    <Grid VerticalOptions="FillAndExpand" BackgroundColor="CornflowerBlue">
        <Label Text="Center me!" Style="{StaticResource H1}" TextColor="White" HorizontalOptions="Center"/>
    </Grid>
</ContentPage.Content>

The title won't center like the ContentPage.Content or like Xamarin does. No matter what I've tried, <Grid>, <HoritonalStackLayout> and every HorizontalOptions the content is always pinned on left and sized exactly fit the text, instead of expanding the <Grid> or <StackLayout> to the window width (WinUI) and centering its children.

The only thing it seems to respond to is WidthRequest but that isn't very useful.

enter image description here


Solution

  • Per your description, this is a known issue that being tracked in the link below, you can follow up there.

    https://github.com/dotnet/maui/issues/10703

    Normally, when setting HorizontalOptions="FillAndExpand" of <Grid> or <HoritonalStackLayout> in WinUI Maui, the TitleView content should have expanded across the entire width of the window width.

    Unfortunately, it failed to work as expected in WinUI Maui. And I also tested in other platforms in Maui, the HorizontalOptions="FillAndExpand" can function well in TitleView content.