views:

935

answers:

2

In my Silverlight app I want a multi-line text box to expand every time the user hits Enter.

The difficult part is how to calculate the correct height based on the number of text lines.

I have tried the following but the textbox becomes too small:

box.Height = box.FontSize*lineCount + box.Padding.Top + box.Padding.Bottom + box.BorderThickness.Top + box.BorderThickness.Bottom;

What am I missing here? Or maybe it can be done automatically somehow?

Thanks, Jacob

Edit: I suspect the problem to be in the FontSize property (does it use another size unit?)

+1  A: 

This seems to be how the textbox works out of the box. Just make sure you set the AcceptsReturn="True" on the textbox. Also make sure you don't set the height of the Textbox so that it is calculated for you.

Bryant
A: 

The TextBox will fire a SizeChanged event, and it will also set the ActualHeight property.

I don't think this was the case in Silverlight 2, when I had to use a TextBlock with the same font, set the padding to 4, and set the same text, and get the ActualHeight off that.

Mike Blandford