Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextBox ignores HorizontalContent/TextAlignment APIs #2896

Open
Felix-Dev opened this issue Jul 13, 2020 · 10 comments
Open

TextBox ignores HorizontalContent/TextAlignment APIs #2896

Felix-Dev opened this issue Jul 13, 2020 · 10 comments
Labels
area-TextBox TextBox, RichEditBox bug Something isn't working team-Controls Issue for the Controls team

Comments

@Felix-Dev
Copy link
Contributor

Felix-Dev commented Jul 13, 2020

Describe the bug
The TextBox control currently ignores the following two APIs to align its text inside of it:

  • HorizontalContentAlignment
  • HorizontalTextAlignment

See the following XAML markup:

<StackPanel>
    <Border BorderThickness="1" BorderBrush="Black" Margin="0,0,0,20">
        <TextBlock Text="TextBlock text" HorizontalTextAlignment="Right" Width="150" />
    </Border>
    
    <TextBox Text="TextBox text" Width="150"  HorizontalTextAlignment="Right" HorizontalContentAlignment="Right"/>
</StackPanel>

And the result:
image

Expected behavior
Text of the TextBox should also be aligned to the right.

Additional context
This bug also affects all the controls using a TextBox internally, such as AutoSuggestBox and NumberBox. This is a gap in WinUI in regards to Win32 APIs like WinForms NumericUpDown control which can align its content to the right just fine:
image

For the NumberBox, an own issue exists in #1722 which would easily be done once this bug has been fixed.

Version Info

Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT
@msft-github-bot msft-github-bot added the needs-triage Issue needs to be triaged by the area owners label Jul 13, 2020
@MikeHillberg
Copy link
Contributor

Can you try working around this by setting TextAlignment rather than HorizontalTextAlignment on the TextBox?

@Felix-Dev
Copy link
Contributor Author

Felix-Dev commented Jul 14, 2020

@MikeHillberg Not sure how I missed that property 😑

image

I also checked the documentation and it states:

This property [HorizontalTextAlignment] provides the same functionality as the TextAlignment property. If both properties are set to conflicting values, the last one set is used.

Apps that target the Fall Creators Update (SDK 16299) or later should use this property instead of TextAlignment.

Edit: I misread the documentation leading me to close this issue. This is an error: The issue is real.

@msft-github-bot msft-github-bot removed the needs-triage Issue needs to be triaged by the area owners label Jul 14, 2020
@MikeHillberg
Copy link
Contributor

No, I think it's still a bug; you should be able to use either property, your sample code looks valid.

I'm confused by the documentation too. It's saying that this is last-writer-wins behavior, but we have a pretty strict rule to not rely on the order that properties are set (too much ambiguity and too little control).

I don't remember how TextBox ended up with this duplicate property, but I think it was anticipating a VerticalTextAlignment property like Xamarin Forms has.

@MikeHillberg MikeHillberg reopened this Jul 14, 2020
@msft-github-bot msft-github-bot added the needs-triage Issue needs to be triaged by the area owners label Jul 14, 2020
@Felix-Dev
Copy link
Contributor Author

Felix-Dev commented Jul 14, 2020

@MikeHillberg Yep, the documentation confused me for a second here which made me think this is a non-issue. Turns out there really still is an issue here. I wrote some test XAML to check when the TextAlignment and HorizontalTextAlignment APIs are applied. See the following XAML and code-behind.

<StackPanel Orientation="Horizontal" Margin="0,20,0,20" HorizontalAlignment="Center">
    <Button Content="Set HorizontalTextAlignment" Click="Button_Click" Margin="0,0,10,0"/>
    <Button Content="Set TextAlignment" Click="Button_Click_1"/>
</StackPanel>

<Border BorderThickness="1" BorderBrush="Black" Margin="0,20,0,20" Grid.Row="3" HorizontalAlignment="Center">
    <TextBlock Text="TextBlock text" HorizontalTextAlignment="Right" Width="150" />
</Border>

<TextBox x:Name="DemoTextBox" 
         Text="TextBox text" Width="150" Grid.Row="4" Margin="0,0,0,20" TextAlignment="Left" HorizontalTextAlignment="Left"/>
private void Button_Click(object sender, RoutedEventArgs e)
{
    this.DemoTextBox.HorizontalTextAlignment = TextAlignment.Right;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    this.DemoTextBox.TextAlignment = TextAlignment.Right;
}

This is how it plays out:
textbox-textalignment

As you can see, setting HorizontalTextAlignment on the TextBox has no effect at all. That API simply appears to be ignored.

This doesn't match the documentation for the TextBox.HorizontalTextAlignment API at all:

Apps that target the Fall Creators Update (SDK 16299) or later should use this property instead of TextAlignment.

There is clearly a disconnect between the documentation and the API behavior (I ran the example code above targeting the 18362 SDK). As it stands now, the HorizontalTextAlignment property is meaningless to set on at least Windows 10 1903 and above and we should figure out if this is a documentation issue or an API issue.

A similar issue happens in the case of the TextBlock as well. The documentation has the same remarks and we also see the API behavior vs documentation mismatch:

<StackPanel Orientation="Horizontal" Margin="0,20,0,20" HorizontalAlignment="Center">
    <Button Content="Set HorizontalTextAlignment" Click="Button_Click" Margin="0,0,10,0"/>
    <Button Content="Set TextAlignment" Click="Button_Click_1"/>
</StackPanel>

<Border BorderThickness="1" BorderBrush="Black" Margin="0,20,0,20" Grid.Row="3" HorizontalAlignment="Center">
    <TextBlock x:Name="DemoTextBlock" Text="TextBlock text" Width="150" HorizontalTextAlignment="Left" TextAlignment="Left"/>
</Border>
private void Button_Click(object sender, RoutedEventArgs e)
{
    this.DemoTextBlock.HorizontalTextAlignment = TextAlignment.Right;
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    this.DemoTextBlock.TextAlignment = TextAlignment.Right;
}

textblock-textalignment

@chrisglein chrisglein added bug Something isn't working needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) and removed needs-triage Issue needs to be triaged by the area owners labels Jul 17, 2020
@StephenLPeters
Copy link
Contributor

@chrisglein or @MikeHillberg do we need to take a follow up to change the documentation in the mean time?

@MikeHillberg
Copy link
Contributor

I'd like to understand first what the details are.

@alexdi220
Copy link
Contributor

Hello guys. Do you have plans to fix it or improve the API? I guess this should be better - there are 3 properties but only the TextAlignment works

<TextBox HorizontalContentAlignment="Center" HorizontalTextAlignment="Center" TextAlignment="Center"/>

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
@fabianoriccardi
Copy link

Issue still valid using WASDK v1.5.5. May the moderators reopen this issue?

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jul 14, 2024
@codendone codendone reopened this Jul 16, 2024
@codendone codendone added team-Controls Issue for the Controls team and removed needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) needs-triage Issue needs to be triaged by the area owners no-issue-activity labels Jul 16, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jul 16, 2024
@codendone codendone removed the needs-triage Issue needs to be triaged by the area owners label Aug 16, 2024
@ranjeshj
Copy link
Contributor

HorizontalContentAlignment is a by product of deriving from Control, but I'm not sure if we need to have both TextAlignment and HorizontalTextAlignment. I think one of these should be removed, but this will be a breaking ABI change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TextBox TextBox, RichEditBox bug Something isn't working team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

9 participants