dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.04k stars 1.16k forks source link

Control's size is incorrect when in a grid column with Width="auto" and MaxWidth="100" #9485

Open yll690 opened 1 month ago

yll690 commented 1 month ago

Description

Control's size is incorrect when in a grid column with Width="auto" and MaxWidth="100"

Reproduction Steps

  1. Create new project targeting net8.0-windows
  2. Add the following code to MainWindow.xaml
    <Grid Width="200">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" MaxWidth="100"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="1111111111111111111111111" TextWrapping="Wrap"/>
        <GridSplitter Width="5" Background="Orange" HorizontalAlignment="Right"/>
    </Grid>
  3. Run the project.

Expected behavior

The TextBlock's width should be 100 and the text should be wrapped. If I drag the GridSplitter, The width of TextBlock becomes correct. image

Actual behavior

The TextBlock's width is its desired width and the text is not wrapped. image

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

No response

lindexi commented 1 month ago

@yll690 See @walterlv 's blog:

hongruiyu commented 1 month ago

To achieve your desired effect, you only need to set the width of the TextBlock.

<TextBlock Text="1111111111111111111111111" Width="auto" MaxWidth="100" TextWrapping="Wrap"/>
waliqadri101 commented 1 month ago

Definitely it's a bug.

miloush commented 1 month ago

"Definitely" is a strong word. The documentation says:

If a child element is added to a column within a Grid, and the column has its Width property set to Auto, the child will be measured without restrictions. This behavior can prevent horizontal scroll bars from displaying if a ScrollViewer is being used, as the child element is measured as unbounded. For purposes of display, the child is clipped rather than scrolled.

That is consistent with the behavior shown.

yll690 commented 1 month ago

@miloush No, the other document says

When you use these values together in the same code example, the MinWidth value takes precedence over the MaxWidth value, which in turn takes precedence over the Width value.

So it shouldn't exceed 100 when MaxWidth="100" is set.

miloush commented 1 month ago

Well the column doesn't exceed it right? You can't see the control beyond 100, it is measured as unbounded and then clipped, like it said.

I am not saying this is right or intuitive, rather pointing out that it is not as clear cut and applications might depend on this (documented) behavior.