+ Reply to Thread
Results 1 to 2 of 2

Thread: calculate WPF TextBlock width

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    14

    calculate WPF TextBlock width

    Let's say I have TextBox with text "Hello" and font size 10
    How can i calculate the TextBlock width?

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    28
    You can use the FormattedText class :
    Code:
    private Size MeasureString(string candidate)
    {
        var formattedText = new FormattedText(
            candidate,
            CultureInfo.CurrentCulture,
            FlowDirection.LeftToRight,
            new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
            this.textBlock.FontSize,
            Brushes.Black);
    
        return new Size(formattedText.Width, formattedText.Height);
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts