Determine the number of line breaks in a TextBlock using a wrapper?

Is there a way to determine the number of line breaks in a text block using TextWrapping="Wrap" ?

I am considering using a non-width font. I need this because I am creating a new and personalized MessageBox window that has a large text title, animation, my application logo and the theme of my application.

It is clear that I need to resize the window according to the number of lines of LineBreaks of the body message - similar to the way the MessageBox window behaves by default.

+7
source share
2 answers

You can see how much txtName.ActualHeight you get without a wrapper, and then divide ActualHeight (with a wrapper) by the value you received earlier. You should get the number of rows.

Note: you will not get the actual height in the constructor. You will receive it after the text block is displayed on the form.

for example: (Nowrap)

 txt.ActualHeight 311.0 

(hoop)

 txt.ActualHeight 1420.4400000000019 

So,

 int lineCount = (txt.ActualHeight / 311.0) 

Hope this helps :)

Update for your update:

If you need to set the height of the messages according to the height of your text block, you can simply do this:

 msgbox.Height = txt.ActualHeight + 10; 

// I added 10 just to add a little margin.

+5
source

Windows can adapt its size based on content. See the SizeToContent property.

+1
source

All Articles