scor4er
scor4er

Reputation: 1589

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.

Upvotes: 0

Views: 1307

Answers (1)

gliderkite
gliderkite

Reputation: 8928

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.

Upvotes: 1

Related Questions