首页 文章

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

提问于
浏览
196

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

16 回答

  • 1

    TextBlock不支持垂直文本对齐 .

    我通过使用Grid包装文本块并设置HorizontalAlignment =“Stretch”和VerticalAlignment =“Center”来解决此问题 .

    像这样:

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

    您可以使用label而不是textblock .

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

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

  • 246

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

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

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

  • 85

    Textblock本身不能进行垂直对齐

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

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

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

  • 2

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

    <TextBlock Height="22" Padding="3" />
    
  • 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"/>
    
  • 2

    就我而言,我这样做是为了让 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"
    
  • 0

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

    enter image description here

  • 0

    Vertically aligned single line TextBox.

    为了扩展@Orion Edwards提供的答案,这就是你如何完全从代码隐藏(没有样式集) . 基本上创建一个继承自Border的自定义类,将其Child设置为TextBox . 下面的示例假设您只需要一行,并且边框是Canvas的子项 . 还假设您需要根据Border的宽度调整TextBox的MaxLength属性 . 下面的示例还将边框的光标设置为模仿文本框,方法是将其设置为“IBeam”类型 . 设置边距“3”,以便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;
            }
        }
    }
    
  • 48

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

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

  • 1

    我认为最好在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
    <TextBox AcceptsReturn="True" 
               TextWrapping="Wrap"  
               VerticalContentAlignment="Top" >
      </TextBox>
    
  • 0

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

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

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

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

相关问题