Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow TextBox to be resized but not to grow on user input

Tags:

wpf

textbox

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)

like image 596
Ray Avatar asked Mar 03 '09 03:03

Ray


2 Answers

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

like image 109
Gishu Avatar answered Oct 21 '22 01:10

Gishu


WPF's TextBox doesn't seem to have that option built-in.

To solve this problem, you can use a custom TextBox that reports a desired (0, 0) size. It's an ugly hack, but it works.

In your MainWindow.xaml.cs file:

namespace NS
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ...
    }

    // Ugly HACK because the regular TextBox doesn't allow autoresize to fit the parent but NOT autoresize when the text doesn't fit.
    public class TextBoxThatDoesntResizeWithText : TextBox
    {
        protected override Size MeasureOverride(Size constraint)
        {
            return new Size(0, 0);
        }
    }
}

Then, in your MainWindow.xaml file:

<Window x:Class="NS.MainWindow"
    ...
    xmlns:local="clr-namespace:NS"
    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>

        <local:TextBoxThatDoesntResizeWithText Grid.Column="1" Grid.Row="0" TextWrapping="WrapWithOverflow" />
    </Grid>
</Window>
like image 36
ajarov Avatar answered Oct 21 '22 00:10

ajarov