Detect the number of line breaks in TextBlock with wrap?
Asked Answered
M

2

9

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.

Mirellamirelle answered 11/4, 2013 at 13:34 Comment(9)
I'm thinking to check the height of the textblock, but I'm not founding a good way to this. I don't think that is the only solution. Sorry for my english.Mirellamirelle
You may take a look at this solution. It's about TextBox, but may still help you.Isham
you can use the ActualHeight propertyConsolidate
Please explain what you're trying to do.Aleydis
It is pretty clear what he is trying to do: find out how many lines are in a wrapped TextBlock. What's unclear?Emplacement
What's unclear is "why the hell do he need that". There is probably another way to do what he's trying to do.Aleydis
@NicolasRepiquet I will update the question.Mirellamirelle
If your issue is to resize a parent window, you shouldn't need to do that. You can set sizes in most controls to auto and have them grow to fit their content.Andel
Please don't prefix your questions with tags, there's a tag system for that. Refer to meta.stackexchange.com/q/19190 for the general discussion. Also, I removed some superfluous text in your question to highlight the question and explain your intent in a short text, so future readers will have an easier task of reading it. Also, you don't have to put "Thanks" in the end of your question, put text like that in your "About me" text on your profile card.Gladsome
C
5

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.

Consolidate answered 11/4, 2013 at 14:3 Comment(2)
Yes, I'll try to do it. I think that is the best way, however if I need to change the font size, I'll need to test again and recalculate again, and I don't want this.Mirellamirelle
@Guilherme: Check my updated answer, you can avoid recalculating.Consolidate
A
2

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

Aleydis answered 11/4, 2013 at 14:11 Comment(1)
Yes, this works with the size of window, but in this specific case, I'll need a "less automatically" way, because of some animations and other controls. Anyway, many thanks for the help. +1Mirellamirelle

© 2022 - 2024 — McMap. All rights reserved.