Vertical Text in Wpf TextBlock

后端 未结 14 1794
星月不相逢
星月不相逢 2020-11-29 04:14

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

14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 04:21

    You could also use the "RUN" binding

    In the App.xaml file use something like this:

    
    
    
        
    
        
                  
                    
                        
                            
                        
                    
                
        
    
    
    

    Create the command class binded to the textblock using i:Interaction.Triggers on the Loaded event in the app.xaml example

    namespace Deridiam.Helper.Commands
    {
    public class HorizontalToVertical
    {
        private ICommand _convertToVerticalCommand;
    
        public ICommand ConvertToVerticalCmd =>
            _convertToVerticalCommand ?? (_convertToVerticalCommand = new RelayCommand(
                    x =>
                    {
                        var tBlock = x as TextBlock;
                        var horizontalText = tBlock.Text;
                        tBlock.Text = "";
    
                        horizontalText.Select(c => c).ToList().ForEach(c =>
                        {
                            if (c.ToString() == " ")
                            {
                                tBlock.Inlines.Add("\n");
                                //tBlock.Inlines.Add("\n");
                            }
    
                            else
                            {
                                tBlock.Inlines.Add((new Run(c.ToString())));
                                tBlock.Inlines.Add(new LineBreak());
                            }
    
    
                        });
                    }));
    }
    }
    

    Finally in the .xaml file where you want the vertical text to be shown

    
    
    

    Will result in:

    V
    e
    r
    t
    i
    c
    a
    l

    T
    e
    x
    t

提交回复
热议问题