How can I prevent wrapping in a WPF FlowDocument in C# code?

二次信任 提交于 2019-12-10 14:49:14

问题


Studies have shown this is the way to prevent wrapping in, say, a Paragraph:

<Paragraph>
    <TextBlock TextWrapping="NoWrap">unwrapping text</TextBlock>
</Paragraph>

How can I get the behavior in C# code?

new Paragraph(new TextBlock()
{
    Text = "unwrapping text",
    TextWrapping = TextWrapping.NoWrap
});

Yields cannot convert from 'System.Windows.Controls.TextBlock' to 'System.Windows.Documents.Inline' because Paragraph's constructor is expecting an Inline.

I can't seem to find a way to convert a TextBlock to an Inline to make Paragraph happy. If it works in XAML, there should be a converter somewhere, right? How do I find it?

I realize the point of a FlowDocument is for things to wrap and flow and be re-sizable, etc. I'm writing some simple reports, just some System.Windows.Documents.Tables of data, and unfortunately the first column that wraps is one which contains dates, so

2013-04-24

Becomes

2013-04-
24

I've been using Runs so far, and while they have some TextBlock properties, they offer no TextWrapping or TextTrimming affordances.


回答1:


You want to add the TextBlock to the inlines, the constructor won't do it for you. I think it would be easiest to just write:

var p = new Paragraph();
p.Inlines.Add(new TextBlock()
{
    Text = "unwrapping text",
    TextWrapping = TextWrapping.NoWrap
});


来源:https://stackoverflow.com/questions/16194569/how-can-i-prevent-wrapping-in-a-wpf-flowdocument-in-c-sharp-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!