Wpf TextBlock 中的竖排文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:06:47  来源:igfitidea点击:

Vertical Text in Wpf TextBlock

c#wpf

提问by MarioH

Is it possible to display the text in a TextBlock vertically so that all letters are stacked upon each other (not rotated with LayoutTransform)?

是否可以垂直显示 TextBlock 中的文本,以便所有字母相互堆叠(不使用 LayoutTransform 旋转)?

采纳答案by Ray Burns

Nobody has yet mentioned the obvious and trivial way to stack the letters of an arbitrary string vertically (without rotating them) using pure XAML:

还没有人提到使用纯 XAML 垂直堆叠任意字符串的字母(不旋转它们)的明显而微不足道的方法:

<ItemsControl
  ItemsSource="Text goes here, or you could use a binding to a string" />

This simply lays out the text vertically by recognizing the fact that the string is an IEnumerable and so ItemsControl can treat each character in the string as a separate item. The default panel for ItemsControl is a StackPanel, so the characters are laid out vertically.

这只是通过识别字符串是 IEnumerable 的事实来垂直布置文本,因此 ItemsControl 可以将字符串中的每个字符视为单独的项目。ItemsControl 的默认面板是 StackPanel,因此字符垂直排列。

Note: For precise control over horizontal positioning, vertical spacing, etc, the ItemContainerStyle and ItemTemplate properties can be set on the ItemsControl.

注意:为了精确控制水平定位、垂直间距等,可以在 ItemsControl 上设置 ItemContainerStyle 和 ItemTemplate 属性。

回答by Micah

I don't think there is a straighforward of doing this withought changing the way the system inherently laysout text. The easiest solution would be to change the width of the textblock and supply a few extra properties like this:

我不认为在不改变系统固有的文本布局方式的情况下这样做是直接的。最简单的解决方案是更改文本块的宽度并提供一些额外的属性,如下所示:

<TextBlock TextAlignment="Center" FontSize="14" FontWeight="Bold" Width="10" TextWrapping="Wrap">THIS IS A TEST</TextBlock>

This is hacky, but it does work.

这是hacky,但它确实有效。

回答by Christoffer Lette

It's doable:

这是可行的:

Your TextBlock's TextAlignmentproperty should be set to Center:

TextBlockTextAlignment属性应设置为Center

<TextBlock Name="textBlock1" TextAlignment="Center" Text="Stacked!" />

Then add NewLines between every character:

然后NewLine在每个字符之间添加s:

textBlock1.Text =
    String.Join(
        Environment.NewLine,
        textBlock1.Text.Select(c => new String(c, 1)).ToArray());

(Uses System.Linqto create an array of strings from the individual characters in the original string. I'm sure there are other ways of doing that...)

(用于System.Linq从原始字符串中的单个字符创建一个字符串数组。我确定还有其他方法可以做到这一点......)

回答by Boyan

Here's a way to insert a '\n' after every character in the text of the TextBlock, that way making it display vertically:

这是一种在 TextBlock 文本中的每个字符后插入 '\n' 的方法,这样可以使其垂直显示:

<TextBlock x:Name="VertTextBlock" Text="Vertical Text" Loaded="VertTextBlock_Loaded"></TextBlock>

Then, in the Loaded event handler, you say:

然后,在 Loaded 事件处理程序中,您说:

TextBlock tb = sender as TextBlock;
StringBuilder sb = new StringBuilder(tb.Text);
int len = tb.Text.Length * 2;

for (int i = 1; i < len; i += 2)
{
    sb.Insert(i, '\n');
}

tb.Text = sb.ToString();

That solution was proposed by Lette, but I believe my implementation incurs less overhead.

该解决方案是由 Lette 提出的,但我相信我的实现会产生较少的开销。

回答by Boyan

<linebreak/> can be used to show data in two lines

回答by esko22

Just in case anybody still comes across this post... here is a simple 100% xaml solution.

以防万一有人仍然看到这篇文章......这里有一个简单的 100% xaml 解决方案。

    <TabControl TabStripPlacement="Left">
        <TabItem Header="Tab 1">
            <TabItem.LayoutTransform>
                <RotateTransform Angle="-90"></RotateTransform>      
            </TabItem.LayoutTransform>
            <TextBlock> Some Text for tab 1</TextBlock>
        </TabItem>
        <TabItem Header="Tab 2">
            <TabItem.LayoutTransform>
                <RotateTransform Angle="-90"></RotateTransform>
            </TabItem.LayoutTransform>
            <TextBlock> Some Text for tab 2</TextBlock>
        </TabItem>
    </TabControl>

回答by denis morozov

create a stackpanel with a bunch ot textblocks that take one char

创建一个带有一堆带一个字符的文本块的堆栈面板

回答by denis morozov

make the text container's max width to allow for one char only and wrap the text:

使文本容器的最大宽度仅允许一个字符并包装文本:

<TextBlock TextWrapping="Wrap" MaxWidth="8" TextAlignment="Center" Text="stack" />

回答by TWood

the accepted answer suggested by Ray Burns does not work for me on .net 4.0. Here is how I did it:

Ray Burns 建议的公认答案在 .net 4.0 上对我不起作用。这是我如何做到的:

pull in the mscorlib

拉入 mscorlib

xmlns:s="clr-namespace:System;assembly=mscorlib"

put in your usercontrol/window/page resources

放入您的用户控件/窗口/页面资源

<s:String x:Key="SortString">Sort</s:String>

and use it like this

并像这样使用它

<ItemsControl ItemsSource="{Binding Source={StaticResource SortString}}" Margin="5,-1,0,0"   />    

hope it helps!

希望能帮助到你!

回答by Venkat

Below XAML code changes the angle of text displayed in a textblock.

下面的 XAML 代码更改文本块中显示的文本角度。

<TextBlock Height="14"
        x:Name="TextBlock1"
        Text="Vertical Bottom to Up" Margin="73,0,115,0" RenderTransformOrigin="0.5,0.5" >
        <TextBlock.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform Angle="-90"/>
                <TranslateTransform/>
            </TransformGroup>
        </TextBlock.RenderTransform>
 </TextBlock>