aw04 aw04 - 14 days ago 8
C# Question

WPF Multiline TextBox Caret Position

I have a multiline

TextBox
that that fills the space of it's containing window (both vertically and horizontally). When I give it focus to begin typing, the caret position is in the very center of the
TextBox
... which seems odd to me.

enter image description here

How can I have this start at the top?

The only thing I've found says to set
VerticalAlignment="Top"
but this has the undesirable effect of the
TextBox
no longer filling the height of its parent.

Here's the code, container is a standard Grid.

<TextBox Grid.Row="0" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True"
Text="{Binding Description}" Margin="5 5 5 0" />

Answer

There are two properties that you can set to fix your problem:

TextBox.TextAlignment Property
Control.VerticalContentAlignment Property

<TextBox Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" 
    Text="{Binding Description}" Margin="5 5 5 0" 
    TextAlignment="Left" VerticalContentAlignment="Top" />
Comments