長文を設定したTextBlockに省略表示させる

2025年1月2日木曜日

C# WPF カスタムコントロール

記事のカテゴリー: C#、.NET 9、WPF

長文を設定したTextBlockを配置すると(とくに行数が多いと)UIが縦に伸びて全体を見渡すことが難しくなります。このようなときに初期状態ではTextBlockのテキストを短縮表示して、末尾に追加したリンクをクリックすることで短縮表示と全体表示を切り替えられるカスタマイズを考えます。

添付ビヘイビアで実装します。

C#:

  1. public static class MaxLinesBehavior
  2. {
  3. private const string DefaultShowOverflowText = "全体を表示する";
  4. private const string DefaultHideOverflowText = "省略する";
  5.  
  6. // Text プロパティの代わりとして使う Text2 添付プロパティ
  7. public static readonly DependencyProperty Text2Property = DependencyProperty.RegisterAttached(
  8. "Text2", typeof(string), typeof(MaxLinesBehavior), new FrameworkPropertyMetadata(null, OnPropertyChanged));
  9.  
  10. public static string? GetText2(DependencyObject obj)
  11. {
  12. return (string?)obj.GetValue(Text2Property);
  13. }
  14.  
  15. public static void SetText2(DependencyObject obj, string? value)
  16. {
  17. obj.SetValue(Text2Property, value);
  18. }
  19.  
  20. // 最大行数の添付プロパティ
  21. public static readonly DependencyProperty MaxLinesProperty = DependencyProperty.RegisterAttached(
  22. "MaxLines", typeof(int), typeof(MaxLinesBehavior), new FrameworkPropertyMetadata(0, OnPropertyChanged));
  23.  
  24. public static int GetMaxLines(DependencyObject obj)
  25. {
  26. return (int)obj.GetValue(MaxLinesProperty);
  27. }
  28.  
  29. public static void SetMaxLines(DependencyObject obj, int value)
  30. {
  31. obj.SetValue(MaxLinesProperty, value);
  32. }
  33.  
  34. // 最大行数を超過したテキストを現在非表示にしているかどうかを取得、または設定する添付プロパティ
  35. public static readonly DependencyProperty HideOverflowProperty = DependencyProperty.RegisterAttached(
  36. "HideOverflow", typeof(bool), typeof(MaxLinesBehavior), new FrameworkPropertyMetadata(true, OnPropertyChanged));
  37.  
  38. public static bool GetHideOverflow(DependencyObject obj)
  39. {
  40. return (bool)obj.GetValue(HideOverflowProperty);
  41. }
  42.  
  43. public static void SetHideOverflow(DependencyObject obj, bool value)
  44. {
  45. obj.SetValue(HideOverflowProperty, value);
  46. }
  47.  
  48. // 超過したテキストを表示するリンクで使うテキストの添付プロパティ
  49. public static readonly DependencyProperty ShowOverflowTextProperty = DependencyProperty.RegisterAttached(
  50. "ShowOverflowText", typeof(string), typeof(MaxLinesBehavior), new FrameworkPropertyMetadata(DefaultShowOverflowText, OnPropertyChanged));
  51.  
  52. public static string GetShowOverflowText(DependencyObject obj)
  53. {
  54. return (string)obj.GetValue(ShowOverflowTextProperty);
  55. }
  56.  
  57. public static void SetShowOverflowText(DependencyObject obj, string value)
  58. {
  59. obj.SetValue(ShowOverflowTextProperty, value);
  60. }
  61.  
  62. // 超過したテキストを隠すリンクで使うテキストの添付プロパティ
  63. public static readonly DependencyProperty HideOverflowTextProperty = DependencyProperty.RegisterAttached(
  64. "HideOverflowText", typeof(string), typeof(MaxLinesBehavior), new FrameworkPropertyMetadata(DefaultHideOverflowText, OnPropertyChanged));
  65.  
  66. public static string GetHideOverflowText(DependencyObject obj)
  67. {
  68. return (string)obj.GetValue(HideOverflowTextProperty);
  69. }
  70.  
  71. public static void SetHideOverflowText(DependencyObject obj, string value)
  72. {
  73. obj.SetValue(HideOverflowTextProperty, value);
  74. }
  75.  
  76. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  77. {
  78. if (d is TextBlock textBlock)
  79. {
  80. UpdateInlines(textBlock);
  81. }
  82. }
  83.  
  84. private static void UpdateInlines(TextBlock textBlock)
  85. {
  86. string? text2 = GetText2(textBlock);
  87. int maxLines = GetMaxLines(textBlock);
  88. bool hideOverflow = GetHideOverflow(textBlock);
  89. textBlock.Inlines.Clear();
  90.  
  91. // (1) Text2 が空の場合
  92. if (string.IsNullOrEmpty(text2))
  93. {
  94. return;
  95. }
  96.  
  97. string[] lines = text2.Split('\n');
  98.  
  99. // (2) Text2 の行数が MaxLines 以内の場合、テキスト全体を表示する
  100. if ((maxLines <= 0) || (lines.Length <= maxLines))
  101. {
  102. textBlock.Inlines.Add(new Run(text2));
  103. }
  104.  
  105. // (3) Text2 の行数が MaxLines より大きい場合
  106. // (3-1) かつ超過したテキストを隠す場合、一部のテキストと表示リンクを表示する
  107. else if (hideOverflow)
  108. {
  109. textBlock.Inlines.Add(new Run(string.Join('\n', lines.Take(maxLines)) + '\n'));
  110. Hyperlink link = new(new Run(GetShowOverflowText(textBlock)));
  111. WeakEventManager<Hyperlink, RoutedEventArgs>.AddHandler(link, nameof(link.Click), ShowOverflowLink_Click);
  112. textBlock.Inlines.Add(link);
  113. }
  114.  
  115. // (3-2) かつ超過したテキストを表示する場合、テキスト全体と非表示リンクを表示する
  116. else
  117. {
  118. textBlock.Inlines.Add(new Run(text2 + '\n'));
  119. Hyperlink link = new(new Run(GetHideOverflowText(textBlock)));
  120. WeakEventManager<Hyperlink, RoutedEventArgs>.AddHandler(link, nameof(link.Click), HideOverflowLink_Click);
  121. textBlock.Inlines.Add(link);
  122. }
  123. }
  124.  
  125. private static void ShowOverflowLink_Click(object? sender, RoutedEventArgs e)
  126. {
  127. if ((sender is not Hyperlink link) || (link.Parent is not TextBlock textBlock))
  128. {
  129. return;
  130. }
  131.  
  132. SetHideOverflow(textBlock, false);
  133. }
  134.  
  135. private static void HideOverflowLink_Click(object? sender, RoutedEventArgs e)
  136. {
  137. if ((sender is not Hyperlink link) || (link.Parent is not TextBlock textBlock))
  138. {
  139. return;
  140. }
  141.  
  142. SetHideOverflow(textBlock, true);
  143. }
  144. }

XAML:

  1. <TextBlock
  2. local:MaxLinesBehavior.Text2="これは長文です。&#10;2&#10;3&#10;4&#10;5"
  3. local:MaxLinesBehavior.MaxLines="3" />