10

I need some help. Don't know if this is possible. I want to have the following:

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBox TextWrapping="Wrap" MinLines ="5"/>
    </Grid>

which is a textbox wrapping inside of a grid column with width *. I want the textbox to take all the width available(hence *) but when a user adds text I want it to wrap when it gets to the end of the line(with the space available).

Currently the above will give a textbox with the entire width but when text is entered the width of the textbox just grows with the text.

I know i can set MaxWidth=?, but the point the column is * is that I don't know what the size of the column is.

I would like to say to the textbox "don't grow, whatever wpf gives you take it and don't increase one more pixel above that".

I think what I want is impossible, because wpf asks the control how big it wants to be and when the user adds more text that goes beyond the boundary it kindly requests more space and goes off expanding its width to infinity.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Jose
  • 10,891
  • 19
  • 67
  • 89

3 Answers3

9

Try binding the MaxWidth property of your TextBox to the ActualWidth property of your starred column (you'll have to name your column to do this). I'm pretty sure I've done something like this in the past.

Something like:

MaxWidth={Binding ElementName=MyColumn, Path=ActualWidth}

Good luck!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
  • This looks ok, but it doesn't work because ActualWidth property of the ColumnDefinition is not a DependencyProperty and MaxWidth will always be 0. Tested just now... – Vale Sep 13 '12 at 10:41
0

Seems to work properly for me when I set Grid.Column="1" on the TextBox.

RandomEngy
  • 14,931
  • 5
  • 70
  • 113
0

In addition to what RandomEngy says about Grid.Column="1", you may also have to set some alignment properties on the Grid itself, particularly if it's set inside another container with different child Stretch behavior (e.g. a StackPanel).

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
micahtan
  • 18,530
  • 1
  • 38
  • 33
  • "Some alignment properties"? Which ones, and to what? – Anthony Dec 22 '10 at 21:55
  • @ Anthony: It depends on how the Grid itself is hosted. The sizing behavior of a WPF element is dependent not only on the element itself, but also on which element is hosting it. To see this in action, create a Button (w/text) inside a StackPanel, inside a Window. Change the Orientation of the StackPanel and observe the difference in sizing behavior of the Button... – micahtan Jan 05 '11 at 16:46