WPF 文本块,文本属性中的换行符

发布于 2024-07-19 02:40:45 字数 336 浏览 7 评论 0原文

有没有办法让 \nTextBlock 中换行?

<TextBlock Text="line1\nLine2" />

或者是否有更好的方法在 Text 属性内强制中间换行?

<LineBreak />

这对我不起作用,它需要是 Text 属性的值,因为文本字符串是从外部源设置的。

我熟悉 LineBreak 但这不是我正在寻找的答案。

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

<TextBlock Text="line1\nLine2" />

Or is there a better way to force a middle line break, inside the Text attribute?

<LineBreak />

This doesn't work for me, it needs to be the value of the Text attribute, because the text string is being set from an outside source.

I'm familiar with LineBreak but it's not the answer I'm looking for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

小矜持 2024-07-26 02:40:46

尝试这个:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>

Try this:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>
寄居者 2024-07-26 02:40:46

我知道这又重新提出了一个老问题,但我也遇到了同样的问题。 我的解决方案是使用 HTML 编码的换行符 ()。

Line1&#10;Line2

好像

1号线
第2行

有关更多 HTML 编码字符,请查看 w3schools

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 (&#10;).

Line1&#10;Line2

Looks like

Line1
Line2

For more of the HTML encoded characters check out w3schools

玩套路吗 2024-07-26 02:40:46

最简单的方法是

blabla <换行符/>> coucou <换行/>> coucou 2

所以你只需编写 XAML 代码, 的含义与 HTML 中的
或“\n”完全相同在 C# 中。

The easiest way is

<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>

So you just write XAML code, and the <LineBreak /> has exactly the same meaning the
in HTML or the "\n" in C#.

冷心人i 2024-07-26 02:40:46

将这一行分成两个标签怎么样?

<StackPanel>
    <TextBlock Text="Line1" />
    <TextBlock Text="Line2" />
</StackPanel>

How about breaking the line into two tags?

<StackPanel>
    <TextBlock Text="Line1" />
    <TextBlock Text="Line2" />
</StackPanel>
沫尐诺 2024-07-26 02:40:46

<换行符> 如果它位于 Grid 或 StackPanel 等集合中,则不起作用。
在这种情况下,以下内容将按所示工作:

LineBreak inside a collection

<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

一腔孤↑勇 2024-07-26 02:40:46

正确的使用方法可能如下:

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

Correct way to use it may be the following :

<TextBlock>  
    <Span>text1</Span>  
    <LineBreak/>  
    <Span>text2</Span>  
</TextBlock>
友谊不毕业 2024-07-26 02:40:46

对于同一文本块中的多行,最适合我的方法是:

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

确保不使用 TextWrapping="Wrap"。 使用 TextWrapping="NoWrap" 或不使用任何内容。

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.

甜是你 2024-07-26 02:40:46
  <HyperlinkButton 
        Content="Apply and restart this pplication!

Note that modifying these settings requires the application to be restarted."   />

CRLF simple way = !

! - 适用于所有 wpf、xaml、silverlight 控件如 TextBlock、HyperlinkText 等

  <HyperlinkButton 
        Content="Apply and restart this pplication!

Note that modifying these settings requires the application to be restarted."   />

CRLF simple way = !

! - Work on all wpf, xaml, silverlight controls like TextBlock, HyperlinkText and more

红墙和绿瓦 2024-07-26 02:40:46

如果您要绑定 TextBlock 的 Text,则其他答案都不起作用。 只需将“\n”添加到要中断的绑定文本中即可。

If you are binding TextBlock's Text, none of the other answers work. Simply add '\n' to the binding text to where you want to break.

凉栀 2024-07-26 02:40:46

当我使用绑定时,这个 对我不起作用。 但这有效:

$"first line {Environment.NewLine} second line"

this &#10; did not work for me, when I used binding. But this works:

quot;first line {Environment.NewLine} second line"
海螺姑娘 2024-07-26 02:40:46

这也可以正常工作:

<TextBlock>
    <Run Text="My nice text"/>
    <LineBreak/>
    <LineBreak/>
    <Run Text="After some linebreaks, I'm back!"/>
</TextBlock>

This also works fine:

<TextBlock>
    <Run Text="My nice text"/>
    <LineBreak/>
    <LineBreak/>
    <Run Text="After some linebreaks, I'm back!"/>
</TextBlock>
作妖 2024-07-26 02:40:46

我参加聚会迟到了但是..
这或多或少是我的做法,(注意我的 ItemSources 是纯字符串,没有格式化,而且我不需要“convertBack”任何东西)

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();
    }
}

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();
    }
}
壹場煙雨 2024-07-26 02:40:46

我遇到了类似的问题,想要将 xaml 标记字符串绑定到 TextBlock。 本质上是将声明性标记存储在 TextBlock 内的字符串中以供以后使用。

这就是我的做法:我对 TextBlock 进行了子类化,以使 InlineCollection 可绑定,并在字符串和 InlineCollection(或者实际上是内联的通用列表)之间编写了一个转换器。

I was having a similar problem and wanted to bind a String of xaml markup to a TextBlock. Essentialy storing the declarative markup inside a TextBlock in a string for later use.

This is how I did: I subclassed the TextBlock to make the InlineCollection bindable and wrote a Converter between the string and an InlineCollection(or actually a generic list of Inlines.)

清醇 2024-07-26 02:40:46

只需使用 AccessText 控件即可。 您可以像标签一样使用它,并且您拥有属性 TextWrapping="WrapWithOverflow"

例如。

我的就是这样并且工作正常。 此外,动态更改文本也没有任何问题。

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.

靑春怀旧 2024-07-26 02:40:46

这也很好用。

使用此方法,我们可以根据需要修改每一行中的文本属性。

<TextBlock>
    <StackPanel>
        <TextBlock FontSize="12" FontWeight="Bold" >My Text One</TextBlock>
        <TextBlock FontFamily="Times New Roman" FontStyle="Italic">
            - My Text Two
        </TextBlock>
    </StackPanel>
</TextBlock>

This also works fine.

Using this method we can modify the text properties in each line as we required.

<TextBlock>
    <StackPanel>
        <TextBlock FontSize="12" FontWeight="Bold" >My Text One</TextBlock>
        <TextBlock FontFamily="Times New Roman" FontStyle="Italic">
            - My Text Two
        </TextBlock>
    </StackPanel>
</TextBlock>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文