tags:

views:

3345

answers:

2

I have a TextBox defined inside a window like so:

<Window x:Class="NS.MainWindow"
    ...
    SizeToContent="WidthAndHeight">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition MinWidth="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="50" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0">Description:</TextBlock>

        <TextBox Grid.Column="1" Grid.Row="0" TextWrapping="WrapWithOverflow" />
    </Grid>
</Window>

The problem is that when the user types in the TextBox it expands to the right since only the MinWidth is set. What I really want is the text to wrap to the next line. I can get it to do this if I change the MinWidth on the column to be Width instead. However if I do this, then the TextBox no longer resizes when the Window is resized.

Is there a way I can have both? (i.e. resize only on Window resize, otherwise wrap)

A: 

Change the second ColumnDefinition to be Width="*".

Donnelle
This will still grow the textbox. Using * implies - use the rest of the available width for Column#2
Gishu
+3  A: 

The reason you're having this behavior is because you've set the Window's SizeToContent property - which basically authorizes the Window to resize itself based on the size requested by its content. So as you type in more stuff, the textbox says I need more space, the window obediently grows. Your textbox would not grow if you don't set the SizeToContent property.

So I'd say lose the SizeToContent property setter & Use proportional grid sizing. Here I say make Column#2 twice the width of Column#1. The default "Stretch" value of HorizontalAlignment and VerticalAlignment for the Grid should ensure that your controls resize correctly on a window resize.

<Window ...
    Title="MyWindow" WindowStyle="ToolWindow" ResizeMode="CanResizeWithGrip"
        MinWidth="300" Width="300" Height="80">
    <Grid x:Name="myGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" MinWidth="100"/>
            <ColumnDefinition Width="2*" MinWidth="200" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition MinHeight="50" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" Grid.Row="0">Description:</TextBlock>
        <TextBox Grid.Column="1" Grid.Row="0" TextWrapping="WrapWithOverflow"/>
    </Grid>

If you just add the SizeToContent property setter back to above code snippet... you'd see some weird behavior where the textbox initially grows with text content.. however if you resize the window once.. the textbox would stop growing. Strange... can't explain that behavior.
HTH

Gishu