wpf
wpf copied to clipboard
Control's size is incorrect when in a grid column with Width="auto" and MaxWidth="100"
Description
Control's size is incorrect when in a grid column with Width="auto" and MaxWidth="100"
Reproduction Steps
- Create new project targeting net8.0-windows
- 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>
- 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.
Actual behavior
The TextBlock's width is its desired width and the text is not wrapped.
Regression?
No response
Known Workarounds
No response
Impact
No response
Configuration
No response
Other information
No response
@yll690 See @walterlv 's blog:
- Chinese: https://blog.walterlv.com/post/the-bugs-of-grid.html
- English: https://blog.walterlv.com/post/the-bugs-of-grid-en.html
To achieve your desired effect, you only need to set the width of the TextBlock.
<TextBlock Text="1111111111111111111111111" Width="auto" MaxWidth="100" TextWrapping="Wrap"/>
Definitely it's a bug.
"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.
@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.
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.