Monday, October 1, 2012

How to create a RichTextBox in C# and measure its size?

How to create a RichTextBox in C# and measure its size?

I want to create multiple RichTextBox to deal with the UIElement 2048px max size restriction on Windows Phone. I'm aware there's a scrollable-textblock solution.

Here's my test code:

RichTextBox rtb = new RichTextBox(); rtb.TextWrapping = TextWrapping.Wrap; rtb.FontSize = (Application.Current.Resources["PhoneFontSizeMedium"] as double?).Value; rtb.FontFamily = Application.Current.Resources["PhoneFontFamilyNormal"] as FontFamily; rtb.Width = this.Width; Run run = new Run(); run.Text = "Hello World.Hello World.Hello World.Hello World.Hello World."; Paragraph para = new Paragraph(); para.Inlines.Add(run); rtb.Blocks.Add(para); double rtbh = rtb.ActualHeight; BodyPanel.Children.Add(rtb); 

BodyPanel is a StackPanel, the application runs fine, however, the ActualHeight property always returns 0 in the debug window.

If I define a RichTextBox in XAML and set its content in C#, its size properties are all normal.

So how can I create a RichTextBox in pure C# and get its size measurements?

BTW, I cannot find any official documentation on the 2048px restriction.

Answers & Comments...




1 comment:

Post a Comment

Send us your comment related to the topic mentioned on the blog