none
How to get ActualHeight and ActualWidth of a TextBlock before i add to parent grid? (in WPF). RRS feed

  • Question

  • I’m trying to create a custom control. Where based on some property value of the my custom control, I need to create some TextBlocks and have to do some calculation based on TextBlock actualHeight and ActualWidth.

    So I need to know the Actual size of the TextBlock before I add them to parent Canvas or grid.

    How to get ActualHeight and ActualWidth of a TextBlock before i add to parent grid?

    Please help...


    somnath
    Monday, June 30, 2008 9:12 PM

Answers

  • That is not a good idea. Consider this: the font family, size and style may change when the text block is added to your parent. In fact the family and size almost certainly will because it will be inherited from your window.

    If you really must try this, you could call Measure on the textblock and look at its DesiredSize before adding it. The actual panel you add it to could still arrange it in a completely different size mind you.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    Monday, June 30, 2008 9:44 PM

All replies

  • That is not a good idea. Consider this: the font family, size and style may change when the text block is added to your parent. In fact the family and size almost certainly will because it will be inherited from your window.

    If you really must try this, you could call Measure on the textblock and look at its DesiredSize before adding it. The actual panel you add it to could still arrange it in a completely different size mind you.
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
    Monday, June 30, 2008 9:44 PM
  • Thanks Tim it would help me a lot...



    somnath
    Tuesday, July 1, 2008 12:18 PM
  • this was a hack, but it helped me out, thanks
    Tuesday, February 15, 2011 10:51 PM