I have a string to draw in a custom dialog box. How can i get the required length of string in pixels using WPF?
Asked
Active
Viewed 5,157 times
2
-
possible duplicate of [Calculate the display width of a string in C#?](http://stackoverflow.com/questions/263614/calculate-the-display-width-of-a-string-in-c) – BrokenGlass Jun 29 '11 at 18:03
-
Please be aware that most calculations in the answers that will be given will not be in pixels but in 1/96 of an inch. – Emond Jun 29 '11 at 19:34
2 Answers
2
If you want to show it afterwards within a TextBlock, create the TextBlock and call Measure and Arrange. Make sure that the TextBlock has set the right font size before calling Measure.
Another way is to go via FormattedText, if you want to do your calculations on a low level.

HCL
- 36,053
- 27
- 163
- 213
-
+1. FormattedText is real nice and even handles the same options as TextBlock such as abbreviating with ... when the string is too long. – Ed Bayiates Jun 29 '11 at 18:09
0
You might not need the (pixel) size.
It might be better to automatically size the dialog to its content.

Emond
- 50,210
- 11
- 84
- 115