字符串属性中的换行符

Moj*_*ter 275 xaml

当将文本设置为属性时,如何为文本添加换行符,即:

<TextBlock Text="Stuff on line1 \n Stuff on line2" />
Run Code Online (Sandbox Code Playgroud)

将其分解为爆炸格式不适合我的特定情况.我需要的是模仿以下内容的一些方法:

<TextBlock>
  <TextBlock.Text>
    Stuff on line1 <LineBreak/>
    Stuff on line2
  </TextBlock.Text>
<TextBlock/>
Run Code Online (Sandbox Code Playgroud)

Bob*_*ing 538

<TextBlock Text="Stuff on line1&#x0a;Stuff on line 2"/>
Run Code Online (Sandbox Code Playgroud)

您可以使用任何十六进制编码的值来表示文字.在这种情况下,我使用了换行符(char 10).如果你想做"经典" vbCrLf,那么你可以使用&#x0d;&#x0a;

顺便说一句,请注意语法:它是&符号,一个磅,字母x,然后是你想要的字符的十六进制值,然后最后是一个分号.

另外:为了完整性,您可以绑定到已嵌入其中的换行符的文本,如后面的代码中的常量或运行时构造的变量.

  • 截至今天,我对这个答案有34个赞成,我仍然不得不回来检查,因为语法太古怪了. (41认同)
  • 显然,这在<TextBlock.Text> </ TextBlock.Text>标签中不起作用. (14认同)
  • 添加反斜杠时甚至可以在绑定参数内部工作:ConverterParameter ='Test \&#x0a; Text' (5认同)
  • 'Archaic'==>'奥术':-) (4认同)
  • @tcables,只需使用`xml:space="preserve"`,然后它也可以在 TextBlock.Text 标签中使用。示例:`&lt;TextBlock xml:space="preserve"&gt;&lt;TextBlock.Text&gt;First Second&lt;/TextBlock.Text&gt;&lt;/TextBlock&gt;` (3认同)
  • 绝对.我有这个书签.我必须一直检查它. (2认同)

scr*_*789 67

可能是您可以使用属性xml:space ="preserve"来保留源XAML中的空格

<TextBlock xml:space="preserve">
Stuff on line 1
Stuff on line 2
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

  • 您还可以删除xml:space ="preserve"并在行之间添加<LineBreak /> (7认同)
  • 应该注意(几年后)`xml:space ="preserve"`还将保留缩进空格,这些空格通常存在于XAML中.因此,如上所述的显式"<LineBreak />"似乎是一个更清洁的解决方案. (5认同)

Jay*_*kul 55

当你需要做一个字符串(例如:在你的资源),你需要使用xml:space="preserve" 与符号字符代码:

<System:String x:Key="TwoLiner" xml:space="preserve">First line&#10;Second line</System:String>
Run Code Online (Sandbox Code Playgroud)

或文中的文字换行符:

<System:String x:Key="TwoLiner" xml:space="preserve">First line 
Second line</System:String>
Run Code Online (Sandbox Code Playgroud)

警告:如果你写的代码就像第二个例子中,你已经插入或者换行符,或者回车和换行,取决于行结束您的操作系统和/或文本编辑器的使用.例如,如果你编写并将其提交到linux系统中的git,一切看起来都不错 - 但如果有人将其克隆到Windows,git会将你的行结尾转换为\r\n你的字符串是什么...你可能打破世界.

当你保留空白时,请注意这一点.如果你写这样的东西:

<System:String x:Key="TwoLiner" xml:space="preserve">
First line 
Second line 
</System:String>
Run Code Online (Sandbox Code Playgroud)

你实际上已经添加了四个换行符,可能是四个回车符,以及可能隐藏的白色空间......

  • 顺便说一句,您也可以将`xml:space ="preserve"属性应用于`ResourceDictionary`,这样您就不需要将它应用于每个资源字符串. (9认同)

S.M*_*avi 20

您只需删除<TextBlock.Text>并简单地添加内容如下:

    <Grid Margin="20">
        <TextBlock TextWrapping="Wrap" TextAlignment="Justify" FontSize="17">
        <Bold FontFamily="Segoe UI Light" FontSize="70">I.R. Iran</Bold><LineBreak/>
        <Span FontSize="35">I</Span>ran or Persia, officially the <Italic>Islamic Republic of Iran</Italic>, 
        is a country in Western Asia. The country is bordered on the 
        north by Armenia, Azerbaijan and Turkmenistan, with Kazakhstan and Russia 
        to the north across the Caspian Sea.<LineBreak/>
        <Span FontSize="10">For more information about Iran see <Hyperlink NavigateUri="http://en.WikiPedia.org/wiki/Iran">WikiPedia</Hyperlink></Span>
            <LineBreak/>
            <LineBreak/>
            <Span FontSize="12">
                <Span>Is this page helpful?</Span>
                <Button Content="No"/>
                <Button Content="Yes"/>
            </Span>
    </TextBlock>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 15

请注意,要执行此操作,您需要在Text属性中执行此操作,您不能使用类似的内容

<TextBlock>Stuff on line1&#x0a;Stuff on line 2</TextBlock>
Run Code Online (Sandbox Code Playgroud)


LPL*_*LPL 12

也许有人更喜欢

<TextBlock Text="{Binding StringFormat='Stuff on line1{0}Stuff on line2{0}Stuff on line3',
                          Source={x:Static s:Environment.NewLine}}" />
Run Code Online (Sandbox Code Playgroud)

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


Cod*_*ick 11

对于那些已经尝试过这个问题的每一个答案但仍然为什么没有为你工作的人而感到头疼的人,你可能遇到了我遇到的问题.

我的TextBlock.Text属性位于ToolTipService.ToolTip元素内部,它被数据绑定到一个对象的属性,该对象的数据是从SQL存储过程中提取的.现在,从SQL函数中提取了存储过程中此特定属性的数据.

由于没有任何对我有用,我放弃了我的搜索,并在下面创建了转换器类:

public class NewLineConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = string.Empty;

        if (value.IsNotNull())
        {
            s = value.ToString();

            if (s.Contains("\\r\\n"))
                s = s.Replace("\\r\\n", Environment.NewLine);

            if (s.Contains("\\n"))
                s = s.Replace("\\n", Environment.NewLine);

            if (s.Contains("&#x0a;&#x0d;"))
                s = s.Replace("&#x0a;&#x0d;", Environment.NewLine);

            if (s.Contains("&#x0a;"))
                s = s.Replace("&#x0a;", Environment.NewLine);

            if (s.Contains("&#x0d;"))
                s = s.Replace("&#x0d;", Environment.NewLine);

            if (s.Contains("&#10;&#13;"))
                s = s.Replace("&#10;&#13;", Environment.NewLine);

            if (s.Contains("&#10;"))
                s = s.Replace("&#10;", Environment.NewLine);

            if (s.Contains("&#13;"))
                s = s.Replace("&#13;", Environment.NewLine);

            if (s.Contains("<br />"))
                s = s.Replace("<br />", Environment.NewLine);

            if (s.Contains("<LineBreak />"))
                s = s.Replace("<LineBreak />", Environment.NewLine);
        }

        return s;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

我最终不得不使用@ dparker的答案中Enivornment.NewLine方法.我指示转换器查找换行符的任何可能的文本表示并替换它.Environment.NewLine

这有效!

但是,我仍然感到困惑的是为什么其他方法都没有使用数据绑定属性.

我对@ BobKing接受的答案发表了评论:

@BobKing - 当绑定到具有从SQL sproc嵌入的换行符的字段时,这似乎不适用于ToolTipService.ToolTip.

他回答说:

@CodeMaverick如果您使用嵌入的新行绑定到文本,它们应该是真正的char 10值(或13),而不是XML标记.仅当您要在XAML文件中编写文字新行时才会这样.

一个灯泡熄灭了!

我进入了我的SQL函数,用...修改了换行符的文本表示.

CHAR( 13 ) + CHAR( 10 )
Run Code Online (Sandbox Code Playgroud)

...从我的TextBlock.Text绑定中删除转换器,就像那样...... 它有效!


Nev*_*lle 9

我发现这有用,但在将其添加到XAML中的"Content = ..."标记时遇到了一些错误.

我在内容中有多行,后来发现内容保留了空格,即使我没有指定.所以为了解决这个问题并让它"忽略"空白,我实现了这样的.

<ToolTip Width="200" Style="{StaticResource ToolTip}" 
         Content="'Text Line 1' 
                   &#x0a;&#x0d;'Text Line 2' 
                   &#x0a;&#x0d;'Text Line 3'"/>
Run Code Online (Sandbox Code Playgroud)

希望这有助于其他人.

(输出有三个文本行,每行之间有一个空行.)


dpa*_*ker 5

我意识到这是一个较旧的问题,但只是想补充一点

环境.NewLine

如果通过代码执行此操作也有效。