How to display the text in one line in wpf textblock

后端 未结 2 1729
情歌与酒
情歌与酒 2021-01-12 07:47

I\'m a newbie with wpf , what i want to display the text in one line in wpf textblock. eg.:

         


        
2条回答
  •  無奈伤痛
    2021-01-12 08:02

    Instead of this:

                
    

    Use this:

                
                    Hello
                    How Are
                    You??
                
    

    or this:

                
                    Hello 
                    How Are 
                    You??
                
    

    or set Text property in code behind like this :

    (In XAML)

                
    

    (In code - c#)

                MyTextBlock.Text = "Hello How Are You??"
    

    Code-behind approach has an advantage that you can format your text before setting it. Example: If the text is retrieved from a file and you want to remove any carriage-return new-line characters you can do it this way:

     string textFromFile = System.IO.File.ReadAllText(@"Path\To\Text\File.txt");
     MyTextBlock.Text = textFromFile.Replace("\n","").Replace("\r","");
    

提交回复
热议问题