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.

Patrick
  • 17,669
  • 6
  • 70
  • 85
Guilherme
  • 5,143
  • 5
  • 39
  • 60
  • 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. – Guilherme Apr 11 '13 at 13:43
  • 1
    You may take a look at this [solution](http://stackoverflow.com/questions/5720302/wpf-get-wrapped-text-out-of-a-textbox). It's about TextBox, but may still help you. – Stefan Over Apr 11 '13 at 13:45
  • you can use the ActualHeight property – Avishek Apr 11 '13 at 13:46
  • Please explain what you're trying to do. – Nicolas Repiquet Apr 11 '13 at 13:48
  • 2
    It is pretty clear what he is trying to do: find out how many lines are in a wrapped TextBlock. What's unclear? – Alex Apr 11 '13 at 13:49
  • 2
    What's unclear is "why the hell do he need that". There is probably another way to do what he's trying to do. – Nicolas Repiquet Apr 11 '13 at 13:53
  • @NicolasRepiquet I will update the question. – Guilherme Apr 11 '13 at 13:57
  • 2
    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. – Matt Burland Apr 11 '13 at 14:04
  • Please don't prefix your questions with tags, there's a tag system for that. Refer to http://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. – Patrick Apr 11 '13 at 15:24

2 Answers2

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.

Avishek
  • 1,896
  • 14
  • 33
  • 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. – Guilherme Apr 11 '13 at 14:10
  • @Guilherme: Check my updated answer, you can avoid recalculating. – Avishek Apr 11 '13 at 14:12
2

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

Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
  • 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. +1 – Guilherme Apr 11 '13 at 14:51