ZoomFontText.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. namespace Muchinfo.WPF.Controls.Zoom
  4. {
  5. /// <summary>
  6. /// Class ZoomFontText.
  7. /// </summary>
  8. public class ZoomFontText : Control
  9. {
  10. static ZoomFontText()
  11. {
  12. DefaultStyleKeyProperty.OverrideMetadata(typeof(ZoomFontText), new FrameworkPropertyMetadata(typeof(ZoomFontText)));
  13. }
  14. public double FontCount
  15. {
  16. get { return (double)GetValue(FontCountProperty); }
  17. set { SetValue(FontCountProperty, value); }
  18. }
  19. // Using a DependencyProperty as the backing store for FontCount. This enables animation, styling, binding, etc...
  20. public static readonly DependencyProperty FontCountProperty =
  21. DependencyProperty.Register("FontCount", typeof(double), typeof(ZoomFontText), new PropertyMetadata(2d));
  22. /// <summary>
  23. /// 放大倍数
  24. /// </summary>
  25. public double ZoomFontSize
  26. {
  27. get { return (double)GetValue(ZoomFontSizeProperty); }
  28. set { SetValue(ZoomFontSizeProperty, value); }
  29. }
  30. // Using a DependencyProperty as the backing store for ZoomSize. This enables animation, styling, binding, etc...
  31. public static readonly DependencyProperty ZoomFontSizeProperty =
  32. DependencyProperty.Register("ZoomFontSize", typeof(double), typeof(ZoomFontText), new PropertyMetadata(2d));
  33. public string NormalText
  34. {
  35. get { return (string)GetValue(NormalTextProperty); }
  36. set { SetValue(NormalTextProperty, value); }
  37. }
  38. // Using a DependencyProperty as the backing store for NormalText. This enables animation, styling, binding, etc...
  39. public static readonly DependencyProperty NormalTextProperty =
  40. DependencyProperty.Register("NormalText", typeof(string), typeof(ZoomFontText), new PropertyMetadata(""));
  41. /// <summary>
  42. /// 放大文本
  43. /// </summary>
  44. public string ZoomText
  45. {
  46. get { return (string)GetValue(ZoomTextProperty); }
  47. set { SetValue(ZoomTextProperty, value); }
  48. }
  49. // Using a DependencyProperty as the backing store for ZoomText. This enables animation, styling, binding, etc...
  50. public static readonly DependencyProperty ZoomTextProperty =
  51. DependencyProperty.Register("ZoomText", typeof(string), typeof(ZoomFontText), new PropertyMetadata(""));
  52. public string Text
  53. {
  54. get { return (string)GetValue(TextProperty); }
  55. set { SetValue(TextProperty, value); }
  56. }
  57. // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
  58. public static readonly DependencyProperty TextProperty =
  59. DependencyProperty.Register("Text", typeof(string), typeof(ZoomFontText), new PropertyMetadata("", OnTextChange));
  60. private static void OnTextChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  61. {
  62. var self = d as ZoomFontText;
  63. if (self == null) return;
  64. // SetViewBoxWidthHeight(self);
  65. var text = e.NewValue as string;
  66. if (string.IsNullOrWhiteSpace(text)) return;
  67. if (text.Length < self.FontCount)
  68. {
  69. self.NormalText = text;
  70. self.ZoomText = string.Empty;
  71. return;
  72. }
  73. self.NormalText = text.Substring(0, text.Length - (int)self.FontCount);
  74. self.ZoomText = text.Substring(text.Length - (int)self.FontCount);
  75. }
  76. }
  77. }