| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 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);
- }
- /// <summary>
- /// 根据字体大小写计算宽度
- /// </summary>
- /// <param name="fontSize">Size of the font.</param>
- /// <param name="text">The text.</param>
- /// <returns>System.Double.</returns>
- public static double GetTextWidth(double fontSize, string text)
- {
- var fontFamily = new FontFamily("Verdana");
- return GetTextWidth(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal, fontSize, text);
- }
- /// <summary>
- /// 根据字体大小写计算宽度
- /// </summary>
- /// <param name="fontFamily">The font family.</param>
- /// <param name="fontSize">Size of the font.</param>
- /// <param name="text">The text.</param>
- /// <returns>System.Double.</returns>
- public static double GetTextWidth(FontFamily fontFamily, double fontSize, string text)
- {
- return GetTextWidth(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal, fontSize, text);
- }
- /// <summary>
- /// 根据字体大小写计算宽度
- /// </summary>
- /// <param name="fontFamily">The font family.</param>
- /// <param name="fontStyle">The font style.</param>
- /// <param name="fontWeight">The font weight.</param>
- /// <param name="fontStretch">The font stretch.</param>
- /// <param name="fontSize">Size of the font.</param>
- /// <param name="text">The text.</param>
- /// <returns>System.Single.</returns>
- 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;
- }
- }
- }
|