简体   繁体   English

多行WPF文本块:行的对齐方式不同

[英]Multiline WPF textblock: different alignment for lines

Is that possible to set different horizontal alignment for different lines of multiline textblock? 可以为多行文本块的不同行设置不同的水平对齐方式吗?

For example, I want to center header of my text, but main text I want to align by left side. 例如,我想使文本的标题居中,但要使左侧的文本居中对齐。 I'd not want to use several textblocks for this issue. 我不想为此问题使用多个文本块。

Thanks. 谢谢。

I do not think you can get what you are looking for using the TextBlock control, this because the Inline elements that you can add in your TextBlock does not allow you to control the horizontal alignment (line by line). 我认为您无法使用TextBlock控件获得所需的内容,这是因为可以在TextBlock中添加的Inline元素不允许您控制水平对齐(逐行)。

Anyway I do not think that this is the best approach, in fact, as specified by MSDN: 无论如何,事实上,按照MSDN的规定,这并不是最好的方法:

TextBlock is not optimized for scenarios that need to display more than a few lines of content; 对于需要显示多行内容的场景,TextBlock并未进行优化。 for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. 在这种情况下,就性能而言,将FlowDocument与适当的查看控件一起使用是比TextBlock更好的选择。 After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. 在TextBlock之后,FlowDocumentScrollViewer是显示流内容的下一个最轻量的控件,它仅提供具有最少UI的滚动内容区域。 FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. FlowDocumentPageViewer已针对流内容的“一次页面”查看模式进行了优化。 Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight. 最终,FlowDocumentReader支持用于查看流内容的最丰富的设置功能,但重量相应地更大。

So if you want to have more flexible control, and also better in terms of performance for what you ask it to do, you should use the FlowDocument . 因此,如果您想拥有更灵活的控制,并且在性能方面要求更高,则应该使用FlowDocument

I want to add a final remark. 我要说最后一句话。 If you're looking for a dramatic improvement in performance, and you think that a single TextBlock is the right solution, I suggest to use Visual elements and the relative DrawText method instead. 如果您正在寻求性能上的显着提高,并且您认为单个TextBlock是正确的解决方案,则建议改用Visual元素和相对的DrawText方法。 But if you're looking for an easy control to maintain in the future the choice is FlowDocument, as already said. 但是,如果您正在寻找将来要维护的简单控件,那么就选择FlowDocument,就像已经说过的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM