Kugel
Kugel

Reputation: 19864

How to set height of textblock/textbox to 3 lines?

I can set fixed height in pixels, but i'd like to set it in lines. Sort of like in html you can set height of an textarea to number of rows/lines.

Upvotes: 14

Views: 7104

Answers (3)

Bryan
Bryan

Reputation: 1471

For a TextBox, set the MinLines and MaxLines properties. To better approximate an HTML textarea, consider also setting TextWrapping, VerticalScrollBarVisibility, and AcceptsReturn as follows:

<TextBox TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" MinLines="3" MaxLines="3"/>

Upvotes: 17

Jonathan ANTOINE
Jonathan ANTOINE

Reputation: 9223

Solution 1

You could FormattedText to measure the size of text, here is an example:

String text = "Here is my text";
Typeface myTypeface = new Typeface("Helvetica");
FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, 
        FlowDirection.LeftToRight, myTypeface, 16, Brushes.Red);

Size textSize = new Size(ft.Width, ft.Height);

Solution 2

Use the Graphics class (found here ):

System.Drawing.Font font = new System.Drawing.Font("Calibri", 12, FontStyle.Bold);
Bitmap bitmap = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(bitmap);
SizeF measureString = g.MeasureString(text, font);

Here you are !

Upvotes: 2

Catch22
Catch22

Reputation: 3351

Try 3em

1em is equal to the current font size. 2em means 2 times the size of the current font. E.g., if an element is displayed with a font of 12 pt, then '2em' is 24 pt.

Upvotes: -3

Related Questions