Monday, December 27, 2021

TextBox won't stretch vertically

The WPF TextBox can be multi line, accept enter, display wrapping, etc. All you need is something like...

<TextBox VerticalAlignment="Stretch" TextWrapping="WrapWithOverflow" Text="{Binding Message}" ScrollViewer.CanContentScroll="True" AcceptsReturn="True"/>

Sometimes this doesn't work and the textbox won't expand.


This may be because you have set a height. If you set a height, VerticalAlignment=Stretch is ignored. In my case, I had a default style that included a height. One way to fix this is to assign an empty style...

<
TextBox VerticalAlignment="Stretch" TextWrapping="WrapWithOverflow" Text="{Binding Message}" ScrollViewer.CanContentScroll="True" AcceptsReturn="True">
    <TextBox.Style>
        <Style TargetType="TextBox"/>
    <TextBox.Style>
</TextBox>



No comments:

Post a Comment