WPF TextBlock中的文本垂直对齐方式

时间:2009-09-29 10:06:33

标签: wpf vertical-alignment textblock

如何为TextBlock中的文本指定垂直居中对齐?我找到了TextAlignment属性,但它用于水平文本对齐。如何进行垂直文本对齐?

17 个答案:

答案 0 :(得分:259)

Textblock本身无法进行垂直对齐

我发现这样做的最好方法是将文本块放在边框内,这样边框就会为你做对齐。

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

注意:这在功能上等同于使用网格,它只取决于您希望控件如何与布局的其余部分相匹配哪一个更合适

答案 1 :(得分:90)

虽然Orion Edwards Answer适用于任何情况,但每次要执行此操作时添加边框并设置边框属性可能会很麻烦。另一种快速方法是设置文本块的填充:

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

答案 2 :(得分:49)

TextBlock不支持垂直文本对齐。

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

像这样:

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

答案 3 :(得分:15)

您可以使用标签而不是文本块。

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

答案 4 :(得分:3)

TextBlock不支持其内容的垂直对齐。如果您必须使用TextBlock,则必须将其与其父级对齐。

但是,如果您可以使用Label代替(并且它们具有非常相似的功能),那么可以定位文本内容:

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

Label默认会拉伸以填充其边界,这意味着标签的文本将居中。

答案 5 :(得分:3)

如果没有text wrapping,我认为用Label替换TextBlock是最简洁的方法。否则请遵循其他有效答案之一。

<Label Content="Some Text" VerticalAlignment="Center"/>

答案 6 :(得分:2)

对我来说,VerticalAlignment="Center"解决了这个问题 这可能是因为TextBlock被包裹在网格中,但实际上wpf中的所有内容都是如此。

答案 7 :(得分:1)

我认为使用没有边框和背景的文本框是到达中心对齐的文本块的便捷方法

<TextBox
TextWrapping="Wrap"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="{x:Null}"
BorderBrush="{x:Null}"
/>

答案 8 :(得分: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"

答案 9 :(得分:1)

如果您可以忽略TextBlock的高度,最好使用它:

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

答案 10 :(得分:1)

只是为了咯咯笑,给这个XAML一个旋转。它并不完美,因为它不是“对齐”,但它允许您在段落中调整文本对齐。

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

答案 11 :(得分:1)

我发现修改文本框样式(即:controltemplate)然后修改PART_ContentHost垂直对齐到中心就可以了。

答案 12 :(得分:0)

我发现我必须做的略有不同。我的问题是,如果我改变了字体大小,文本将在TextBox中向上移动,而不是留在底部,其余的TextBoxes就行了。通过从顶部到底部更改垂直对齐,我能够以编程方式将字体从大小20更改为大小为14&amp;返回,将文本的重力保持在底部并保持整洁。方法如下:

enter image description here

答案 13 :(得分:0)

Vertically aligned single line TextBox.

为了扩展@Orion Edwards提供的答案,您可以完全使用代码隐藏(没有设置样式)。基本上创建一个继承自Border的自定义类,将其Child设置为TextBox。下面的示例假设您只需要一行,并且边框是Canvas的子项。还假设您需要根据Border的宽度调整TextBox的MaxLength属性。下面的示例还将边框的光标设置为模拟文本框,方法是将其设置为类型&#39; IBeam&#39;。边际为&#39; 3&#39;设置为使TextBox不与边框左边绝对对齐。

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;
        }
    }
}

答案 14 :(得分:0)

我认为最好在Label中使用Label(或TextBlock),你不能直接在边框控件中附加鼠标事件,最后它附加在TextBlock中,这是我的推荐:

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

答案 15 :(得分:0)

  <TextBox AcceptsReturn="True" 
           TextWrapping="Wrap"  
           VerticalContentAlignment="Top" >
  </TextBox>

答案 16 :(得分:0)

你可以看到我的博文。您可以从代码隐藏设置Textblock的自定义高度。要设置自定义高度,您需要将其设置在边框或堆栈面板中

http://ciintelligence.blogspot.com/2011/02/wpf-textblock-vertical-alignment-with.html

相关问题