摘要:Silverlight Measure String Size in Pixels
namespace MyProject.Extensions
{
public static class StringExtension
{
// FontSize : 0 will not apply size calculation
// FontFamily: null will not apply size calculation
// FontWeight is not nullable, default use FontWeights.Normal
public static Size Measure(this string strText, double dbFontSize, FontFamily fontFamily, FontWeight fontWeight)
{
var tb = new TextBlock();
if (dbFontSize > 0)
tb.FontSize = dbFontSize;
if (fontFamily != null)
tb.FontFamily = fontFamily;
tb.FontWeight = fontWeight;
tb.Text = strText;
var tb2 = tb;
return new Size(tb2.ActualWidth, tb2.ActualHeight);
}
}
}
Usage:
string strTest = "9999";
Size size = strTest.Measure(12, new FontFamily("Tahoma"), FontWeights.Normal);
ctrlAA.Height = size.Height;