Skip to content

AutoLayout children have conflicting HorizontalAlignment behavior #1361

Open
@erikvilima

Description

@erikvilima

Current behavior

If an AutoLayout has children and they have HorizontalAlignment set to Center they won't display at the center, but at the left side. However if you dynamically (eg. button click) change their HorizontalAlignment to Center they will move to the center.


An UI update makes the AutoLayout children return to the left. For example, given the following XAML:

<Grid>
    <utu:AutoLayout Orientation="Vertical">
        <TextBlock x:Name="categoriesTb"
                   TextAlignment="Right"
                   HorizontalAlignment="Center"
                   Text="Categories"
                   utu:AutoLayout.CounterAlignment="Start"
                   Foreground="{ThemeResource OnBackgroundBrush}" />
        <TextBlock x:Name="tacTb"
                   TextAlignment="Right"
                   HorizontalAlignment="Center"
                   utu:AutoLayout.CounterAlignment="Start"
                   Foreground="{ThemeResource OnBackgroundBrush}">
            TEXT AS CONTENT
        </TextBlock>
        <utu:AutoLayout Spacing="8"
                        x:Name="MyAutoLayout"
                        Orientation="Horizontal">
            <Button Content="Change Alignment"
                    Click="Button_Click_2" />
            <Button Content="Change Orientation"
                    Click="Button_Click" />
        </utu:AutoLayout>
    </utu:AutoLayout>
</Grid>
  1. When the app starts, the TextBlocks are aligned to the left.
  2. I click the "Change Alignment" button, which sets the TextBlocks' HorizontalAlignment to Center via C#—this works, and they align to the center.
  3. I click the "Change Orientation" button, which changes MyAutoLayout's orientation to Vertical.
  4. The orientation change applies successfully, but the TextBlocks return to the left.
Recording.2025-03-05.175150.mp4

There's a power struggle between the AutoLayout Counter/PrimaryAlignment properties and the Horizontal/VerticalAlignments somewhere and they aren't being respected in the proper order of precedence.

Expected behavior

There shouldn't be a conflict in HorizontalAlignment in AutoLayout children. Questions that need answer:

  1. Should HorizontalAlignment have effect on elements inside an AutoLayout?
  2. Shouldn't only utu:AutoLayout.PrimaryAlignment and utu:AutoLayout.CounterAlignment be used to modify elements alilgnment inside an AutoLayout?

How to reproduce it (as minimally and precisely as possible)

Repro

hd2034.zip <<<<<<<<<<<<<<<<<<<<<<

Nuget Package:
Uno.Toolkit.WinUI:6.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething isn't workingtriage/untriagedIndicates an issue requires triaging or verification.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions