using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace MuchInfo.Chart.Infrastructure.Helpers { public static class TreeHelper { public static object TemplateFindName(string name, FrameworkElement templatedParent) { return ((Control)templatedParent).Template.FindName(name, templatedParent); } /// /// 根据字体大小写计算宽度 /// /// Size of the font. /// The text. /// System.Double. public static double GetTextWidth(double fontSize, string text) { var fontFamily = new FontFamily("Verdana"); return GetTextWidth(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal, fontSize, text); } /// /// 根据字体大小写计算宽度 /// /// The font family. /// Size of the font. /// The text. /// System.Double. public static double GetTextWidth(FontFamily fontFamily, double fontSize, string text) { return GetTextWidth(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal, fontSize, text); } /// /// 根据字体大小写计算宽度 /// /// The font family. /// The font style. /// The font weight. /// The font stretch. /// Size of the font. /// The text. /// System.Single. public static double GetTextWidth(FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize, string text) { var formattedText = new FormattedText(text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(fontFamily, fontStyle, fontWeight, fontStretch), fontSize, Brushes.Black); return formattedText.Width; } } }