microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.38k stars 683 forks source link

TextBox TextWrapping WrapWholeWords causes xml parsing exception #4199

Open nzstevem opened 3 years ago

nzstevem commented 3 years ago

Describe the bug Simple MainWindow XML code as follows. NO code behind. When run in VS generated XML parsing exception.

<Window x:Class="Test77.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Test77" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="400"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="400"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" BorderBrush="Aqua" BorderThickness="2" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Top">
        <TextBox TextWrapping="Wrap" Text="# Column ZERO Column ZERO Column ZERO Column ZERO Column ZERO #"/>
    </Border>
    <Border Grid.Column="1" BorderBrush="Aqua" BorderThickness="2" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Top">
        <TextBox TextWrapping="WrapWholeWords" Text="# Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE Column ONE #"/>
    </Border>
    <Border Grid.Column="2" BorderBrush="Aqua" BorderThickness="2" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Top">
        <TextBox TextWrapping="Wrap" Text="# Column TWO Column TWO Column TWO Column TWO Column TWO Column TWO #"/>
    </Border>
</Grid>

Steps to reproduce the bug

  1. Create a new WinUI Desktop app.
  2. Replace MainWindow XAML with the above. Note "WrapWholeWords" on center col TextBox.
  3. Run the app in VS and it will crash. See screen shot.
  4. Changing "WrapWholeWords" to simply "Wrap" and the exception doesn't occur. The Wrap option actually wraps whole words rather than splitting words which might be a bug as well.

Expected behavior Expected the app to run and demonstrate resizing and re-wrapping of center column.

Also noticed that when the app is run with the center column TextWrapping="Wrap" that the TextBox and Border shrink as the page window is reduced as expected. But when the window is then expanded back the center Border/TextBox does not expand back to its original size.

Screenshots

Exception screen... image

Running app with WrapWholeWords changed to Wrap looks as expected image

Second image after window reduced in width and then expanded - the center TextBox doesn't re-size and re-wrap???? image

Version Info Edition Windows 10 Pro Version 20H2 Installed on ‎16/‎07/‎2020 OS build 19042.804 Experience Windows Feature Experience Pack 120.2212.551.0

NuGet package version: [Microsoft.WinUI 3.0.0-preview4.210210.4] -->

Windows app type: UWP Win32
Windows 10 version Saw the problem?
October 2020 Update (19042)
Device form factor Saw the problem?
Desktop

Additional context

StephenLPeters commented 3 years ago

@RealTommyKlein and @fabiant3 FYI

@nzstevem is this issue exclusive to Preview4?

nzstevem commented 3 years ago

I'm not absolutely sure if the issue was introduced with Preview4 but I think NOT. From memory I applied PV4 thinking that might clear the issue. Now I've installed PV4 in VS I'm not sure how to go back.

codendone commented 3 years ago

The error message could definitely be better, but WrapWholeWords is an unsupported value for TextBox. From TextBox.TextWrapping docs:

TextBox and RichEditBox don't support the WrapWholeWords value for their TextWrapping properties. If you try to use WrapWholeWords as a value for TextBox.TextWrapping or RichEditBox.TextWrapping, an exception is thrown.

nzstevem commented 3 years ago

The situation is made worse by the fact that VS doesn't complain during either edit or build.

I've struck a number of issues of this type where code flies through VS intellisense and then build and then fails during execution with an error message that gives no clue as to where the issue lies.

Even in code the TextWrapping Enum includes the WrapWholeWords item. Perhaps it needs a TextBoxTextWrapping Enum to avoid confusion with TextBlock.

image

It took me hours to work out what was causing the issue. Given it had sailed through VS I immediately assumed it was a library/version issue or a bug. After messing around and updating to very latest I then started peeling out options and eventually found by a process of elimination what was triggering the error. These sort of issues are a real headache when you are still trying to get to grips with the framework as I am.

BUT...

The real confusion here is that TextWrapping="Wrap" actually wraps whole words! it does NOT character wrap!!!!

image

So why WholeWords is not supported when Wrap does just this is confusing. It is Character wrap that is not supported!

codendone commented 3 years ago

You're right, it does seem like it should reject Wrap rather than WrapWholeWords. It could probably also be argued that it should just accept either value even if it only wraps as whole words currently -- that might be better than rejecting a value only via exception at runtime.

nzstevem commented 3 years ago

I would have thought an option to just wrap at a character level with no sensitivity to "words" or spaces would be a simple and obvious first option.

The more complex option (which appears to be what we have) is to wrap on word boundaries. I note in a TEXTBLOCK with the WrapWholeWords option when the word won't fit on its own line it truncates the long word and the next line starts with the next word. You wouldn't want to be keying into a TextBox and not be able to see what you entered! So this option might be better named "OnlyWrapAtWordEnd".

A TextBox WrapWholeWords needs to wrap on space when it can but be prepared to character wrap when there is no other choice.

This is like a lot of things. More complex the longer you look at it! :-)

knightmeister commented 2 years ago

This is still an issue as of the current version of WinUI3 (via Windows App SDK)

cmonto commented 1 year ago

Its totally unacceptable that a simple character wrap is not possible in WinUI ATM. The TextWrapping.Wrap does word wrap wasting a lot of space and TextWrapping.WrapWholeWords does a miserable wrap. Just see below to believe this is the same multi line TextBlock with WordEllipsis and same Text.. only the TextWrapping property changed:

Wrap

WrapWholeWords

Abe-Froman commented 11 months ago

Just found this open issue, glad too but I wasted about an hour trying to debug this XAML parsing error before finding it.