1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
public static class MeasureHelper
{
private static TextBlock MeasureTextBlock = new TextBlock();
public static Size MeasureString(string Text, FontFamily FontFamily, double FontSize)
{
//---> Vérouillage du TextBlock car en Silverlight ou se retrouve très vite
// dans un context multi-thread.
lock (MeasureTextBlock)
{
return MeasureString(MeasureTextBlock, Text, FontFamily, FontSize);
}
}
public static Size MeasureString2(string Text, FontFamily FontFamily, double FontSize)
{
var tb = new TextBlock();
return MeasureString(tb, Text, FontFamily, FontSize);
}
private static Size MeasureString(TextBlock tb, string Text, FontFamily FontFamily, double FontSize)
{
tb.Text = Text;
tb.FontSize = FontSize;
tb.FontFamily = FontFamily;
return new Size(tb.ActualWidth, tb.ActualHeight);
}
} |
Partager