Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBlock vs TextBox Padding - XAML WPF

Tags:

c#

wpf

xaml

I'm trying to add padding to a textblock and a textbox on the same horizontal alignment (so the text for both starts at the same horizontal location), but it seems to me that the padding for a textblock vs a textbox is off?

I have this code:

<TextBox Text="Test"
             BorderThickness="0"
             Margin="0"
             Padding="12,0,0,0" />
<TextBlock Text="Test"
           Margin="0"
           Padding="12,0,0,0" />

But on my display, is shows up as this (Stackpanel): stackpanel

Grid: grid

Esentially, my question is, the padding is the same, so what's up with the horizontal alignment?

like image 233
Thomas Avatar asked Oct 22 '25 04:10

Thomas


1 Answers

Padding represents the distance between the side of the control (which can be the margin) and its content.

Well using your Xaml, I used the Snoop utilty and found out that the Textbox has more inner control. For example border,TextBoxView etc... Refer the below image. enter image description here

But, in case of TextBlock, there were none. One of the control in Textbox control is TextBoxView, checking out the margin it has 2,0,2,0.

enter image description here

I changed the Margin = -1,0,0,0 and the content padded properly. So the content was padding against the textBoxView. and making the Margin of the textBoxView equal to -1,0,0,0. it appeared correct with TextBlock alignment.

enter image description here

like image 96
Sats Avatar answered Oct 24 '25 20:10

Sats



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!