tags:

views:

22

answers:

2

Hi,

i have a textblock where i dynamically add string.. even if i add string width and update the textblock the textblock is not showing appropriate width, still some text are cut..

how to measure width that has to be displayed in textblock? and how to make it autosize?

+1  A: 

Textblocks that have no height or width specified will expand automatically until they fill their container. So try that.

Mamta Dalal
That's correct and there is no need to take a dependency on System.Drawing
TimothyP
A: 

You can get the size of a text using these solutions :

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 !

Jmix90