How to stretch TextBox vertically?

Emon Haque 3,176 Reputation points
2021-05-05T08:24:28.247+00:00

The Address in Tenant section, Description in Space as well as Head sections and Business of Lease section are multiline TextBox with these properties:

inputBox.AcceptsReturn = true;  
inputBox.TextWrapping = TextWrapping.Wrap;  
inputBox.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;  
inputBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;  
//inputBox.SetValue(ScrollViewer.CanContentScrollProperty, true);  
//inputBox.VerticalAlignment = VerticalAlignment.Stretch;  
inputBox.VerticalAlignment = VerticalAlignment.Top;  

93903-test.gif

When I resize the window all those TextBoxes stretch except the Address of Tenant section because I've this in MeasureOverride:

protected override Size MeasureOverride(Size availableSize) {  
    if (IsMultiline)   
        inputBox.Height = availableSize.Height == double.PositiveInfinity ? 100 : availableSize.Height;  
      
    container.Width = availableSize.Width;  
    container.Measure(availableSize);  
    return container.DesiredSize;  
}  

for the Address, I've set the Height to 100 otherwise I get System.ArgumentException: ''∞' is not a valid value for property 'Height'.' The Tenant view is inside a ScrollViewer with these properties:

var viewer = new ScrollViewer() {  
    HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,  
    VerticalScrollBarVisibility = ScrollBarVisibility.Auto,  
    //CanContentScroll = true,  
    Content = grid  
};  

and the RowDefinition of the the address TextBox is *.

How to stretch that vertically?

Developer technologies Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. Emon Haque 3,176 Reputation points
    2021-05-07T11:08:59.897+00:00

    @DaisyTian-MSFT, this is the structure I'm using:

    <Grid>  
        <Grid.Resources>  
            <Style TargetType="TextBox">  
                <Setter Property="Margin" Value="5 5 5 5"/>  
            </Style>  
            <Style TargetType="Border">  
                <Setter Property="Margin" Value="5"/>  
                <Setter Property="BorderThickness" Value="1"/>  
            </Style>  
        </Grid.Resources>  
        <Grid.ColumnDefinitions>  
            <ColumnDefinition/>  
            <ColumnDefinition/>  
            <ColumnDefinition/>  
        </Grid.ColumnDefinitions>  
        <Grid.RowDefinitions>  
            <RowDefinition/>  
            <RowDefinition/>  
        </Grid.RowDefinitions>  
        <Border BorderBrush="Black">  
            <ScrollViewer   
                VerticalScrollBarVisibility="Auto"  
                HorizontalScrollBarVisibility="Disabled">  
                <Grid>  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="*"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                        <RowDefinition Height="Auto"/>  
                    </Grid.RowDefinitions>  
                    <TextBox Grid.Row="0" />  
                    <TextBox Grid.Row="1" />  
                    <TextBox Grid.Row="2" />  
                    <TextBox Grid.Row="3" />  
                    <TextBox Grid.Row="4" />  
                    <TextBox Grid.Row="5"   
                             AcceptsReturn="True" TextWrapping="Wrap"  
                             VerticalScrollBarVisibility="Auto"  
                             HorizontalScrollBarVisibility="Disabled"  
                             Text="TextBox that doesn't stretch vertically"/>  
                    <TextBox Grid.Row="6" />  
                    <TextBox Grid.Row="7" />  
                    <Separator Grid.Row="8" Background="SkyBlue"/>  
                    <TextBlock Grid.Row="9" Text="Error"/>  
                </Grid>  
            </ScrollViewer>  
        </Border>  
    
        <Border Grid.Row="1" BorderBrush="Blue">  
            <Grid>  
                <Grid.RowDefinitions>  
                    <RowDefinition Height="Auto"/>  
                    <RowDefinition/>  
                </Grid.RowDefinitions>  
                <TextBox Grid.Row="0" />  
                <TextBox Grid.Row="1"  
                     AcceptsReturn="True" TextWrapping="Wrap"  
                     VerticalScrollBarVisibility="Auto"  
                     HorizontalScrollBarVisibility="Disabled"  
                     Text="TextBox that does stretch vertically"/>  
            </Grid>  
        </Border>  
    </Grid>  
    

    Here in the shortened version, the 6th Multiline TextBox of the first Border doesn't stretch vertically as long as the VerticalScrollbar of the ScrollViewer is visible:

    94640-test.gif
    But the 2nd Multiline TextBox of the second Border does.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.