简体   繁体   English

TextBlock与TextBox填充-XAML WPF

[英]TextBlock vs TextBox Padding - XAML WPF

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): StackPanel中

Grid: 网格: 格

Esentially, my question is, the padding is the same, so what's up with the horizontal alignment? 本质上,我的问题是,填充是相同的,那么水平对齐是怎么回事?

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. 很好地使用您的Xaml,我使用了Snoop实用程序,发现Textbox具有更多内部控件。 For example border,TextBoxView etc... Refer the below image. 例如border,TextBoxView等...请参见下图。 在此处输入图片说明

But, in case of TextBlock, there were none. 但是,如果是TextBlock,则没有。 One of the control in Textbox control is TextBoxView, checking out the margin it has 2,0,2,0. TextBox控件中的控件之一是TextBoxView,它检查出具有2,0,2,0的边距。

在此处输入图片说明

I changed the Margin = -1,0,0,0 and the content padded properly. 我更改了保证金= -1,0,0,0并正确填充了内容。 So the content was padding against the textBoxView. 因此,内容被填充到textBoxView上。 and making the Margin of the textBoxView equal to -1,0,0,0. 并使textBoxView的Margin等于-1,0,0,0。 it appeared correct with TextBlock alignment. 它与TextBlock对齐方式似乎正确。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM