我怎么垂直分配中心对中文本内的一个文本块?我发现或设置的财产,但是对于横向的文本准。我该怎么做呢为垂直的文本对?

有帮助吗?

解决方案

一个正文块本身不能做垂直取向

要做到这一点,我已经找到了最好的办法是把文本块的边界内,所以边框为你做的对齐方式。

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

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

其他提示

虽然猎户座爱德华兹回答任何情况下工作,它可能是一个痛苦的添加边框和设置的属性边境每次你想这样做。另一种快速的方式是设置文本块的填充:

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

该文本块不支持垂直的文本准。

我的工作围绕着这个包裹的文本块格和设置HorizontalAlignment="拉"和方式(="中心"。

是这样的:

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

可以使用标签来代替文本块。

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

如果您可以在没有文本换行做,我认为有一个标签替换TextBlock的是做到这一点的最简洁的方式。否则遵循其他有效的回答中的一个。

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

TextBlock不支持的内容的垂直对齐。如果你必须使用TextBlock那么你必须相对于其父对齐。

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

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

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

对于我来说,VerticalAlignment="Center"修复了这个问题。结果 这可能是因为TextBlockis包裹在一个网格,但那么实际上在WPF的一切。

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

就为了笑声,给这XAML一个旋转。因为它是不是一个“定位”,但它可以让你在段落中调整文本对齐方式,是不完美的。

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

如果你能忽视TextBlock的高度,这是更好地为你使用这样的:

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

在我的情况下,我这样做是为了使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"

您可以看到我的博客文章。您可以从代码隐藏设置文本块的自定义高度。对于设置自定义高度,你需要在一个边框内设置或StackPanel的

http://ciintelligence.blogspot.com/ 2011/02 / WPF的正文块-垂直取向with.html

我发现我不得不这样做略有不同。我的问题是,如果我改变了字体大小,文本将在文本框上移,而不是停留在与文本框就行了,其余的底部。通过从顶部改变VERT对准底部我是能够从大小20编程更改字体大小14回,保持在底部文本的重力和保持事物整洁。方法如下:

“在这里输入的图像描述”

“垂直排列的一行文本框。”

要扩大对@Orion爱德华兹提供的答案,这是怎么了,你会完全从后台代码做(尚未设定样式)。基本上,创建一个从边境有其儿童设置为一个TextBox继承的自定义类。下面的例子假设你只想要一个单一的线和边界是Canvas的孩子。还假设您需要调整基础上,边框的宽度TextBox的MaxLength属性。下面也是示例边界的光标设置通过将其设置到该类型的“I梁”模仿一个文本框。的“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;
        }
    }
}

我觉得是更好地使用标签(或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>
  <TextBox AcceptsReturn="True" 
           TextWrapping="Wrap"  
           VerticalContentAlignment="Top" >
  </TextBox>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top