Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

textblock

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.

like image 286
Kugel Avatar asked Sep 23 '10 10:09

Kugel


2 Answers

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"/>
like image 164
Bryan Avatar answered Nov 15 '22 23:11

Bryan


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 !

like image 29
Jonathan ANTOINE Avatar answered Nov 16 '22 00:11

Jonathan ANTOINE