Wpf TextBlock中的垂直文本

Mar*_*ioH 43 c# wpf

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

Ray*_*rns 73

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

<ItemsControl
  ItemsSource="Text goes here, or you could use a binding to a string" />
Run Code Online (Sandbox Code Playgroud)

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

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

  • 我在Visual Studio 2010中使用了这个,但是在2013年它出现了错误,TypeConverter字符串不支持IEnumerable (6认同)
  • 我粘贴在<ItemsControl ItemsSource ="Text转到这里,或者你可以使用绑定到字符串"/>但是我得到TypeConverter for IEnumberable不支持从字符串转换?我正在使用visual studio 2015,c#wpf (4认同)
  • 通过创建字符串资源并将其绑定到Items Source来克服固定字符串的"不转换为字符串"问题:<Expander.Resources> <system:String x:Key ="Title"> Stacked text </ system: String> </ Expander.Resources> <Expander.Header> <ItemsControl ItemsSource ="{StaticResource Title}"> </ Expander.Header> (2认同)

esk*_*o22 22

以防任何人仍然遇到这篇文章......这是一个简单的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>
Run Code Online (Sandbox Code Playgroud)


Mic*_*cah 18

我认为通过改变系统固有的文本方式来实现这一目标并不是一件好事.最简单的解决方案是更改文本块的宽度并提供一些额外的属性,如下所示:

<TextBlock TextAlignment="Center" FontSize="14" FontWeight="Bold" Width="10" TextWrapping="Wrap">THIS IS A TEST</TextBlock>
Run Code Online (Sandbox Code Playgroud)

这很hacky,但确实有效.


lun*_*tix 11

只需使用简单的LayoutTransform ..

<Label Grid.Column="0" Content="Your Text Here" HorizontalContentAlignment="Center">
  <Label.LayoutTransform>
    <TransformGroup>
        <RotateTransform Angle="90" />
        <ScaleTransform ScaleX="-1" ScaleY="-1"/>
    </TransformGroup>
  </Label.LayoutTransform>
</Label>
Run Code Online (Sandbox Code Playgroud)