Multiline WPF textblock: different alignment for lines

你离开我真会死。 提交于 2019-12-12 03:08:42

问题


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.


回答1:


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).

Anyway I do not think that this is the best approach, in fact, as specified by MSDN:

TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight.

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.

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. But if you're looking for an easy control to maintain in the future the choice is FlowDocument, as already said.



来源:https://stackoverflow.com/questions/10257450/multiline-wpf-textblock-different-alignment-for-lines

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