2009-09-29 299 views
179

如何将垂直中心对齐指定给TextBlock内的文本?我发现TextAlignment属性,但它是用于水平文本对齐。我如何做垂直文本对齐?WPF TextBlock中的文本垂直对齐

+0

@shr和其他人:请注意'TextAlignment'只影响水平对齐,而不影响垂直对齐(如问题所指)。 – 2015-04-24 15:46:29

回答

223

一个TextBlock本身不能做垂直对齐

要做到这一点,我已经发现的是把文本块边界内的最佳方式,所以边界为你做准备。

<Border BorderBrush="{x:Null}" Height="50"> 
    <TextBlock TextWrapping="Wrap" Text="Some Text" VerticalAlignment="Center"/> 
</Border> 

注:这是功能上等同于使用一个网格,它只是取决于你想怎么控制,以配合您的布局的其余部分为哪一个更适合

+18

+1必须设置边框的高度才能使垂直对齐生效。 – 2010-12-24 18:04:40

+13

另外,TextBlock不能有指定的高度,或者它不会垂直居中。 – pearcewg 2010-12-31 21:53:10

+17

@gav - TextAlignment只能进行水平对齐...问题是关于垂直对齐 – 2012-04-11 20:39:02

46

TextBlock不支持垂直文本对齐。

我通过用网格包装文本块并设置Horizo​​ntalAlignment =“Stretch”和VerticalAlignment =“Center”来解决这个问题。

像这样:

<Grid> 
     <TextBlock 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Center" 
      Text="Your text" /> 
    </Grid> 
+0

+1与基于边界的方法一样,甚至不需要为网格设置高度。 – 2012-07-21 15:22:17

+0

我发现这种方法最适合我。我通过在'Grid'内的'Ellipse'上叠加'TextBlock'来创建动态指示灯。无需绑定我的宽度和高度属性或做任何棘手的事情。 – paddy 2013-06-18 03:20:34

1

对于我来说,VerticalAlignment="Center"解决了这个问题。
这可能是因为TextBlock被包装在一个网格中,但在wpf中几乎所有东西都是如此。

76

虽然Orion Edwards Answer适用于任何情况,但每次您想要添加边框并设置边框属性可能会很痛苦。另一种快速方法是设置文本块的填充:

<TextBlock Height="22" Padding="3" /> 
+11

我认为这是最明智的答案。 – 2011-11-23 20:54:01

+1

我被找了!!!谢谢! – 2014-04-16 17:26:55

+1

只有在字体大小为16px的情况下才能使用!? – C4u 2016-08-15 09:06:54

14

您可以使用标签来代替文字块的。

<Label Content="Hello, World!"> 
    <Label.LayoutTransform> 
     <RotateTransform Angle="270"/> 
    </Label.LayoutTransform> 
</Label> 
+3

不错,标签有VerticalContentAlignment。 Greeeat。 +1 – 2012-06-15 17:42:33

+3

不清楚OP是否需要使用TextBlock或可以使用Label。使用标签为我所需要的工作。 +1 – 2013-08-02 17:58:44

+18

这回答了如何产生垂直文本的问题,而不是如何应用垂直对齐! – 2015-09-08 14:27:43

1

我发现,修改文本样式(即:controltemplate),然后修改PART_ContentHost垂直对齐中心将做的伎俩

+0

OP是问关于TextBlocks。他们没有ControlTemplates。 – ANeves 2016-07-12 16:37:52

1

只是为了笑声,给这个XAML一抡。它并不完美,因为它不是“对齐”,但它允许您调整段落内的文本对齐方式。

<TextBlock> 
    <TextBlock BaselineOffset="30">One</TextBlock> 
    <TextBlock BaselineOffset="20">Two</TextBlock> 
    <Run>Three</Run>    
    <Run BaselineAlignment="Subscript">Four</Run> 
</TextBlock> 
1

如果可以俯瞰TextBlock的高度,这是更好地为你使用这样的:

<TextBlock Height="{Binding}" Text="Your text" 
TextWrapping="Wrap" VerticalAlignment="Center" Width="28"/> 
1

就我而言,我这样做是为了使TextBlock显示更好。

<Border BorderThickness="3" BorderBrush="Yellow" CornerRadius="10" Padding="2" 
    HorizontalAlignment="Center" VerticalAlignment="Center" Height="30" Width="150"> 
     <TextBlock FontSize="20" Height="23" HorizontalAlignment="Left" Margin="0,0,0,-5" Text="" VerticalAlignment="Top" Width="141" Background="White" /> 
</Border> 

,使文本进一步从底部的关键是要设置

Margin="0,0,0,-5" 
1

,我发现我不得不这样做略有不同。我的问题是,如果我改变了字体大小,那么文本会在文本框中向上移动,而不是停留在文本框其余部分的底部。通过从上到下更改垂直对齐方式,我能够以编程方式将字体从大小20更改为大小14 &,将文本的重心保持在底部并保持整洁。具体方法如下:

enter image description here

1

Vertically aligned single line TextBox.

要由@Orion爱德华兹提供了答案扩大,这是怎么了,你会完全从后台代码做(尚未设定样式)。基本上创建一个自定义类,该类继承自将其子设置为TextBox的Border。下面的例子假设你只需要一行代码,并且边框是Canvas的子代。还假定您需要根据边框的宽度调整文本框的MaxLength属性。下面的示例还将边框的光标设置为“IBeam”类型,以模仿文本框。设置“3”的边距,以便文本框不完全对齐边界的左侧。

double __dX = 20; 
double __dY = 180; 
double __dW = 500; 
double __dH = 40; 
int __iMaxLen = 100; 

this.m_Z3r0_TextBox_Description = new CZ3r0_TextBox(__dX, __dY, __dW, __dH, __iMaxLen, TextAlignment.Left); 
this.Children.Add(this.m_Z3r0_TextBox_Description); 

类:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Shapes; 
using System.Windows.Controls.Primitives; 


namespace ifn0tz3r0Exp 
{ 
    class CZ3r0_TextBox : Border 
    { 
     private TextBox m_TextBox; 

     private SolidColorBrush m_Brush_Green = new SolidColorBrush(Colors.MediumSpringGreen); 
     private SolidColorBrush m_Brush_Black = new SolidColorBrush(Colors.Black); 
     private SolidColorBrush m_Brush_Transparent = new SolidColorBrush(Colors.Transparent); 

     public CZ3r0_TextBox(double _dX, double _dY, double _dW, double _dH, int _iMaxLen, TextAlignment _Align) 
     { 

      ///////////////////////////////////////////////////////////// 
      //TEXTBOX 
      this.m_TextBox = new TextBox(); 
      this.m_TextBox.Text = "This is a vertically centered one-line textbox embedded in a border..."; 
      Canvas.SetLeft(this, _dX); 
      Canvas.SetTop(this, _dY); 
      this.m_TextBox.FontFamily = new FontFamily("Consolas"); 
      this.m_TextBox.FontSize = 11; 
      this.m_TextBox.Background = this.m_Brush_Black; 
      this.m_TextBox.Foreground = this.m_Brush_Green; 
      this.m_TextBox.BorderBrush = this.m_Brush_Transparent; 
      this.m_TextBox.BorderThickness = new Thickness(0.0); 
      this.m_TextBox.Width = _dW; 
      this.m_TextBox.MaxLength = _iMaxLen; 
      this.m_TextBox.TextAlignment = _Align; 
      this.m_TextBox.VerticalAlignment = System.Windows.VerticalAlignment.Center; 
      this.m_TextBox.FocusVisualStyle = null; 
      this.m_TextBox.Margin = new Thickness(3.0); 
      this.m_TextBox.CaretBrush = this.m_Brush_Green; 
      this.m_TextBox.SelectionBrush = this.m_Brush_Green; 
      this.m_TextBox.SelectionOpacity = 0.3; 

      this.m_TextBox.GotFocus += this.CZ3r0_TextBox_GotFocus; 
      this.m_TextBox.LostFocus += this.CZ3r0_TextBox_LostFocus; 
      ///////////////////////////////////////////////////////////// 
      //BORDER 

      this.BorderBrush = this.m_Brush_Transparent; 
      this.BorderThickness = new Thickness(1.0); 
      this.Background = this.m_Brush_Black;    
      this.Height = _dH; 
      this.Child = this.m_TextBox; 
      this.FocusVisualStyle = null; 
      this.MouseDown += this.CZ3r0_TextBox_MouseDown; 
      this.Cursor = Cursors.IBeam; 
      ///////////////////////////////////////////////////////////// 
     } 
     private void CZ3r0_TextBox_MouseDown(object _Sender, MouseEventArgs e) 
     { 
      this.m_TextBox.Focus(); 
     } 
     private void CZ3r0_TextBox_GotFocus(object _Sender, RoutedEventArgs e) 
     { 
      this.BorderBrush = this.m_Brush_Green; 
     } 
     private void CZ3r0_TextBox_LostFocus(object _Sender, RoutedEventArgs e) 
     { 
      this.BorderBrush = this.m_Brush_Transparent; 
     } 
    } 
} 
0

我认为这是更好地使用标签(或TextBlock的)到一个标签,你不能直接在边境控制连接鼠标事件,最后它被连接TextBlock中的,这是我recomendation:

<Label 
    Height="32" 
    VerticalContentAlignment="Center" 
    HorizontalContentAlignment="Stretch" 
    MouseLeftButtonUp="MenuItem_MouseLeftButtonUp"> 
    <TextBlock Padding="32 0 10 0"> 
     Label with click event 
    </TextBlock> 
</Label> 
1

如果你没有做text wrapping,我认为有一个标签替换TextBlock的是做到这一点的最简洁的方式。否则,请按照其他有效答案之一。

<Label Content="Some Text" VerticalAlignment="Center"/> 
0
<TextBox AcceptsReturn="True" 
      TextWrapping="Wrap" 
      VerticalContentAlignment="Top" > 
    </TextBox> 
+0

问题是'TextBlock',而不是'TextBox'。 -1 – KnorxThieus 2017-08-20 11:02:07

0

TextBlock不支持其内容的垂直对齐方式。如果您必须使用TextBlock,那么您必须将其与其父项对齐。

但是如果你可以使用Label代替(和他们有很相似的功能),那么你可以位置的文本内容:

<Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center"> 
    I am centred text! 
</Label> 

Label将伸展在默认情况下,以填补其边界,这意味着标签的文字将居中。