Text Wrapping Extends a Column to Match Text

I defined the grid simply:

<Grid Margin="0,5,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"></ColumnDefinition> <ColumnDefinition Width="50"></ColumnDefinition> <ColumnDefinition Width="48"></ColumnDefinition> <ColumnDefinition Width="Auto"></ColumnDefinition> </Grid.ColumnDefinitions> 

Then I try to link some content like this:

 <TextBlock TextWrapping="Wrap" Grid.Column="3" Text="{Binding Text}"> 

Set so the text will not wrap. It just expands the column to fit the text. If in the last column I set a fixed value for the width, the hyphenation will work as expected. The problem is that if the user expands the window, the column remains a fixed size. How can I get the size of a column dynamically with a grid width, but still wrap the text in it?

+4
source share
5 answers

The width "*" will evenly distribute the remaining space between the columns using "*" . If you have one column with Width="*" , this column will get all the remaining space. If you have 2 columns with Width="*" , each of them will get 1/2 of the remaining space.

Here's a good article on grid size , which includes star size.

+5
source

There is one disappointing case that I discovered that can break even with Width="*" , and thats when you have IsSharedSizeScope = true .

 <Border BorderBrush="Red" BorderThickness="1"> <StackPanel Grid.IsSharedSizeScope="True"> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="G1"/> <ColumnDefinition Width="Auto" SharedSizeGroup="G2" /> <ColumnDefinition Width="*" SharedSizeGroup="G3" /> </Grid.ColumnDefinitions> <TextBlock Text="Col0" Grid.Column="0" Margin="0,0,5,0"/> <TextBlock Text="Col1" Grid.Column="1" Margin="0,0,5,0"/> <TextBlock Text="ABCDEFGHIJKLMNOPQRSTU VWXYZ" TextWrapping="Wrap" Grid.Column="2"/> </Grid> </StackPanel> </Border> 

This will not be completed, but if you change Grid.IsSharedScopeSize to false , it will happen.

A solution has not yet been found, but this may be another reason why it will not work.

+5
source

Try the following:

 <Grid Margin="0,5,0,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"></ColumnDefinition> <ColumnDefinition Width="50"></ColumnDefinition> <ColumnDefinition Width="48"></ColumnDefinition> <ColumnDefinition Name="ParentColumn" Width="Auto"></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock TextWrapping="Wrap" Grid.Column="3" Text="{Binding Text}" MaxWidth="{Binding ActualWidth, ElementName=ParentColumn}"> 
+1
source

Set its width to "*"

-1
source

You should use Auto only if you want the column / row to fit according to the content in the specified column / row. If you want to "assign the rest of the space", use "*". In your case, the TextBlock should know how much space it has before the acutal measure, so it can determine where to wrap the text.

-1
source

All Articles