WPF Textblock, linebreak in Text attribute

后端 未结 15 1206
南方客
南方客 2021-02-03 16:37

Is there a way to have \\n make a line break in a TextBlock?


Or is there a

相关标签:
15条回答
  • 2021-02-03 17:01

    just use the AccessText control. you can use it like a label and you have the property TextWrapping="WrapWithOverflow"

    eg.

    Mine is like that and it's working fine. Also, you don't have any problems on changing the text dinamically.

    0 讨论(0)
  • 2021-02-03 17:05

    this 
 did not work for me, when I used binding. But this works:

    $"first line {Environment.NewLine} second line"
    
    0 讨论(0)
  • 2021-02-03 17:07

    <LineBreak/> will not work if it is inside a collection such as Grid or StackPanel. In such cases the following would work as shown:

    LineBreak inside a collection

    0 讨论(0)
  • 2021-02-03 17:09

    I know this is ressurecting an old question, but I had the same problem. The solution for me was to use HTML encoded line feeds (&amp;#10;).

    Line1&amp;#10;Line2
    

    Looks like

    Line1
    Line2

    For more of the HTML encoded characters check out w3schools

    0 讨论(0)
  • 2021-02-03 17:10

    The Best way that worked for me for multiple lines in the same Textblock is:

    <TextBlock>  
        text1  
        <LineBreak/>  
        text2  
    </TextBlock>
    

    Make sure to not use TextWrapping="Wrap". Use TextWrapping="NoWrap" or use nothing.

    0 讨论(0)
  • 2021-02-03 17:13

    I'm late to the party but .. this is more or less how I did it ,(mind my ItemSources are plain strings, not formatted , and I didn't need to 'convertBack' anything)

    public class SpaceToLineBreakConverter : IValueConverter
    {   
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            return (!String.IsNullOrEmpty(value as string)) 
            ? new Regex(@"\s").Replace(value as string, "\n") 
            : value;            
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    0 讨论(0)
提交回复
热议问题