views:

1226

answers:

5

I've got a grid defined 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'm trying to bind some content like this:

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

Set up like this, the text won't wrap. It simply expands the column to fit the text. If I set the Width to a fixed amount on the last column, wrapping works as expected. The problem there is that if the user widens the window, the column stays at a fixed size. How can I get the column to size dynamically with the width of the grid, but still wrap the text within it?

A: 

I thought you had to set MultiLine = true also, but I don't see that option anymore...

John Weldon
A: 

Set its width to "*"

Tom
+2  A: 

A width of "*" will split any remaining space evenly between the columns using "*". If you have a single column with Width="*", that column will get all remaining space. If you have 2 columns with Width="*", each will get 1/2 of the remaining space.

Here's a good article on grid sizing that includes star sizing.

Bryan
A: 

Try this:

<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}">
Will
A: 

You should use Auto only when you want to column/row to size depending on content in said column/row. If you want to "asign rest of space" use "*". In your case, TextBlock needs to know how much space it has before acutal measure, so it can tell where to wrap the text.

Euphoric