TreeHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Globalization;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace MuchInfo.Chart.Infrastructure.Helpers
  6. {
  7. public static class TreeHelper
  8. {
  9. public static object TemplateFindName(string name, FrameworkElement templatedParent)
  10. {
  11. return ((Control)templatedParent).Template.FindName(name, templatedParent);
  12. }
  13. /// <summary>
  14. /// 根据字体大小写计算宽度
  15. /// </summary>
  16. /// <param name="fontSize">Size of the font.</param>
  17. /// <param name="text">The text.</param>
  18. /// <returns>System.Double.</returns>
  19. public static double GetTextWidth(double fontSize, string text)
  20. {
  21. var fontFamily = new FontFamily("Verdana");
  22. return GetTextWidth(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal, fontSize, text);
  23. }
  24. /// <summary>
  25. /// 根据字体大小写计算宽度
  26. /// </summary>
  27. /// <param name="fontFamily">The font family.</param>
  28. /// <param name="fontSize">Size of the font.</param>
  29. /// <param name="text">The text.</param>
  30. /// <returns>System.Double.</returns>
  31. public static double GetTextWidth(FontFamily fontFamily, double fontSize, string text)
  32. {
  33. return GetTextWidth(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal, fontSize, text);
  34. }
  35. /// <summary>
  36. /// 根据字体大小写计算宽度
  37. /// </summary>
  38. /// <param name="fontFamily">The font family.</param>
  39. /// <param name="fontStyle">The font style.</param>
  40. /// <param name="fontWeight">The font weight.</param>
  41. /// <param name="fontStretch">The font stretch.</param>
  42. /// <param name="fontSize">Size of the font.</param>
  43. /// <param name="text">The text.</param>
  44. /// <returns>System.Single.</returns>
  45. public static double GetTextWidth(FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize, string text)
  46. {
  47. var formattedText = new FormattedText(text,
  48. CultureInfo.CurrentUICulture,
  49. FlowDirection.LeftToRight,
  50. new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
  51. fontSize,
  52. Brushes.Black);
  53. return formattedText.Width;
  54. }
  55. }
  56. }