Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect the number of line breaks in TextBlock with wrap?

Is there any way to detect the number of lines breaks in a textblock with TextWrapping="Wrap"?

I am considering using a non-monospaced font. I need this because I'm creating a new and personalized MessageBox Window, which has a big text title, animations, the logo of my application and the theme of my application.

It's clear that I need to change the size of the window according to the number of LineBreaks of the body message - similar to how the default MessageBox window behaves.

like image 993
Guilherme Avatar asked Apr 11 '13 13:04

Guilherme


1 Answers

You can see how much txtName.ActualHeight you are getting with no wrap, and then divide the ActualHeight (with wrap) by the value you got earlier. You should get the number of lines.

Note: you wont get the Actual height in the constructor. You will get it after the textblock get rendered in the form.

eg: (NoWrap)

txt.ActualHeight
311.0

(Wrap)

txt.ActualHeight
1420.4400000000019

So,

int lineCount = (txt.ActualHeight / 311.0)

Hope it helps :)

Update as per your question update:

If you need to set messagebox height as per your textblock height, you can simply do this:

msgbox.Height = txt.ActualHeight + 10;

// I added 10 just for adding a little margin.

like image 129
Avishek Avatar answered Oct 14 '22 08:10

Avishek