Home > Software engineering >  WPF textbox selection vertical centering or alignment
WPF textbox selection vertical centering or alignment

Time:12-25

I have started to learn WPF and my first task has been to explore the most basic controls, such as the textbox. It has caught my attention as in WPF everything is much more customizable and powerful, but it has caught my attention how the design of the default textbox differs from those of Winforms, acquiring a rather ugly design.

In the following image we see a textbox with the selected text. One textbox with Winforms and one textbox with WPF. winforms and wpf texbox selection differences

As you can see, in Winforms the upper and lower highlighting space (highlighted in violet ) of the selection is distributed equally, while in WPF the upper part is much larger giving a rather "ugly" appearance to the textboxes when the text is selected.

I would like to know if there is any way to fix this visual misalignment, because I really don't understand why they have left so much space at the top of the text selection.

CodePudding user response:

To center the content, in this case the text, you can use VerticalContentAlignment.

<TextBox VerticalContentAlignment="Center"... />

CodePudding user response:

You can adjust the inner padding by setting Padding property as follows.

<TextBox Padding="0,-2,0,0" ... />

Edit: Respond to OP's comment

  • Related