Search code examples
.netwpfsilverlight-4.0textblock

Silverlight, wpf measure TextBlock text problem!


I cannot get proper desired size of my TextBlock.

I have few lines of code:

TextBlock block = new TextBlock();
        block.Style = this.TextStyle;
        block.UpdateLayout();
        block.Measure(new Size(this.ActualWidth, this.ActualHeight));
        block.Text = "3333";

        return block.DesiredSize;

And i cannot solve two problems with it:

  1. block.DesiredSize.Width is always zero. When height calculated properly.

  2. Desired Size not changes when i set setters for style "TextStyle" For example setter set for FontSize with value 50.

         <Setter Property="FontSize"
                Value="50">
        </Setter>
    

    But Desired height only 15! What wrong with this measure method? And how can i get real size of text block?


Solution

  • Try this

    TextBlock block = new TextBlock();
        block.Text = "3333";
        block.Style = this.TextStyle;
    
        block.Measure(new Size(this.ActualWidth, this.ActualHeight));
        block.UpdateLayout();
    
    
        return block.DesiredSize;