VerticalTextBlock.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. namespace Muchinfo.WPF.Controls
  15. {
  16. /// <summary>
  17. /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
  18. ///
  19. /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
  20. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  21. /// 元素中:
  22. ///
  23. /// xmlns:MyNamespace="clr-namespace:Muchinfo.WPF.Controls"
  24. ///
  25. ///
  26. /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
  27. /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
  28. /// 元素中:
  29. ///
  30. /// xmlns:MyNamespace="clr-namespace:Muchinfo.WPF.Controls;assembly=Muchinfo.WPF.Controls"
  31. ///
  32. /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
  33. /// 并重新生成以避免编译错误:
  34. ///
  35. /// 在解决方案资源管理器中右击目标项目,然后依次单击
  36. /// “添加引用”->“项目”->[浏览查找并选择此项目]
  37. ///
  38. ///
  39. /// 步骤 2)
  40. /// 继续操作并在 XAML 文件中使用控件。
  41. ///
  42. /// <MyNamespace:Vertical/>
  43. ///
  44. /// </summary>
  45. [TemplatePart(Name = VerticalTextBlock.c_TextBlock, Type = typeof(TextBlock))]
  46. public class VerticalTextBlock : Control
  47. {
  48. public const string c_TextBlock = "TextBlock";
  49. static VerticalTextBlock()
  50. {
  51. DefaultStyleKeyProperty.OverrideMetadata(typeof(VerticalTextBlock), new FrameworkPropertyMetadata(typeof(VerticalTextBlock)));
  52. }
  53. public override void OnApplyTemplate()
  54. {
  55. base.OnApplyTemplate();
  56. _textBlock = GetTemplateChild("TextBlock") as TextBlock;
  57. CreateVerticalText(_text);
  58. }
  59. private string _text { get; set; }
  60. private TextBlock _textBlock { get; set; }
  61. public string Text
  62. {
  63. get { return (string)GetValue(TextProperty); }
  64. set { SetValue(TextProperty, value); }
  65. }
  66. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  67. "Text", typeof(string), typeof(VerticalTextBlock), new PropertyMetadata(OnTextChanged));
  68. private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
  69. {
  70. ((VerticalTextBlock)o).OnTextChanged((string)(e.NewValue));
  71. }
  72. private void OnTextChanged(string newValue)
  73. {
  74. CreateVerticalText(newValue);
  75. }
  76. private void CreateVerticalText(string text)
  77. {
  78. _text = text;
  79. if (null != _textBlock)
  80. {
  81. bool first = true;
  82. foreach (var c in _text)
  83. {
  84. if (!first)
  85. {
  86. _textBlock.Inlines.Add(new LineBreak());
  87. }
  88. _textBlock.Inlines.Add(new Run { Text = c.ToString() });
  89. first = false;
  90. }
  91. }
  92. }
  93. }
  94. }