locked
Is there any equivalent to Graphics.MeasureString in WPF? RRS feed

  • Question

  • Hi! I'm a newbie to WPF, and although it seems like a great platform, I am having trouble correlating Windows Forms capabilities to Presentation Foundation methods.

    I would like to know if there is any way to measure the physically-displayed text content of a control (say, for simplicity, a TextBlock). In WinForms I could define the ClipRect and use MeasureString or get the Region to determine the displayed length; however, I cannot find any references to similar graphics methods in WPF.

    My current intent is to enable a ToolTip if the displayed text is truncated, but ultimately I am interested in learning more about format control and the like. (I am also somewhat illiterate in XAML, though I do understand the basics; however, I would imagine that the solution - if there is one - would be done in the code-behind... Of course, I could be wrong...).

    Any help would be greatly appreciated!
    Thursday, March 5, 2009 1:45 AM

Answers

  • You can use the TextBox.ExentWidth property to find the horizontal size of the scrollable content and the TextBox.ViewportWidth property to get the size of viewport. See here


    There are many options which you can take to set the tooltip based on the above information,
    - On MouseHover event of the textbox you can compare the 2 and set the tooptip from code
    - In XAML, you can use bind the ToolTip's text using a value conveter.  See here
    - Using Style triggers also (I know this is possible but just don't know the semnatics for it, so kept it as last option). You can use this option and set this particular behavior application wide.

    Hope this helps
    • Proposed as answer by ABitSmart Thursday, March 5, 2009 10:14 AM
    • Marked as answer by Tao Liang Monday, March 9, 2009 10:08 AM
    Thursday, March 5, 2009 10:13 AM