0

I recently experienced a strange behavior of text boxes.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="200"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Label Content="Test"/>
    <TextBox Grid.Column="1" TextWrapping="Wrap" Text="testtesttesttesttesttesttesttesttesttesttest"/>
    <Control Grid.Row="1" Grid.ColumnSpan="2" Width="280"/>
</Grid>

In this example code I have a Grid with 2 rows and 2 columns. The 2nd column is set to a fixed width. The TextBox has TextWrapping set to Wrap.

In the second row I have a Control with a fixed width that is higher than the Grid would normally be. This increases the ActualWidth of the second column from 200 to about 250.

The actual width of the TextBox also increases to match the new width of the column.

When I now add a long string to the text box the text, it doesn't use the full width of the TextBox but instead wraps way to early and leaves about 40 pixels at the end of the TextBox empty.

I've found out that the TextBox has a readonly ExtentWidth property. This property is responsible for the wrapping. In my example the values of the ExtentWidth is about 180, which is the 200 from the width of the grid column minus margins and paddings.

What can I do to fix the wrapping in the TextBox?

EDIT: This question is not a duplicate of Looking for explanation for WPF Grid ColumnSpan behavior. That question explains what happens to the Widths of the grid columns. But it doesn't answer the question regarding the wrapping behavior of the TextBox.

Community
  • 1
  • 1
raznagul
  • 355
  • 2
  • 19
  • That behavior seems not to be dependent on the Width of the `Control`. It is `Grid.ColumnSpan="2"` which causes this. I don't know if this is willed or not?! `Grid.ColumnSpan` could also be an autocorrection mistake. – Jens Nov 09 '15 at 13:25
  • Possible duplicate of [Looking for explanation for WPF Grid ColumnSpan behavior](http://stackoverflow.com/questions/5679081/looking-for-explanation-for-wpf-grid-columnspan-behavior) – Jens Nov 09 '15 at 13:29
  • @Jens Horstmann: The columns span was set intentionally. And yes, it is part of the problem. As for the possible duplicate, see my edit. – raznagul Nov 09 '15 at 14:22

1 Answers1

0

I honestly don't understand the "why" part, but until someone provides a meaningful explanation, here are some workarounds:

  • Set an explicit width on the TextBox
  • Set MaxWidth on the column instead of Width
  • Bind the width of the TextBox to that of another element inside the same column. e.g:

    <TextBox Grid.Column="1" Width="{Binding ActualWidth, ElementName=rect}" ... /> <Rectangle Grid.Column="1" x:Name="rect"/>

Note that the last one is the only usable workaround if you need to be able to change the column width at runtime and want the TextBox (and the wrapping) to change with it.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • I know this hack and find it ugly. I set the width of the data grid column, so I would not need it. – raznagul Nov 09 '15 at 14:13
  • yes, I was careful to choose the word "hack". Unless you want to give an explicit width to the textbox, or an explicit MaxWidth to the column, this is the only workaround I could find. If someone provides a better answer (I'm sure they will), I will delete this answer. The textbox seems to miss some sort of notification. If you put a gridsplitter next to it, the wrapping problem still exists at the beginning, but as soon as you move the splitter, the problem goes away. I've tried doing a InvalidateArrange and InvalidateMeasure on size change, but that didn't work either. – Eren Ersönmez Nov 09 '15 at 14:25
  • Using MaxWidth instead of Width fixes the problem for me. If add that to the answer, I will accept it. – raznagul Nov 09 '15 at 14:30