/
BindableRichTextBox.cs
122 lines (100 loc) · 4.19 KB
/
BindableRichTextBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace WordWrappingRichTextBox.Views
{
public class BindableRichTextBox : RichTextBox
{
#region Fields
private readonly float _dpiX;
#endregion
#region Dependency Properties
public static readonly DependencyProperty BindingDocumentProperty = DependencyProperty.Register("BindingDocument", typeof(FlowDocument),
typeof(BindableRichTextBox), new UIPropertyMetadata(null, BindingDocumentChanged));
public static readonly DependencyProperty WordWrappingProperty = DependencyProperty.Register("WordWrapping", typeof(bool),
typeof(BindableRichTextBox), new UIPropertyMetadata(true, WordWrappingChanged));
#endregion
#region Properties
public FlowDocument BindingDocument
{
get => (FlowDocument)GetValue(BindingDocumentProperty);
set => SetValue(BindingDocumentProperty, value);
}
public bool WordWrapping
{
get => (bool)GetValue(WordWrappingProperty);
set => SetValue(WordWrappingProperty, value);
}
#endregion
#region Event Methods
/// <summary>
/// A callback function that is executed when the value of the BindingDocument is changed.
/// </summary>
/// <param name="sender">The object that issued the event.</param>
/// <param name="e">Event parameter.</param>
private static void BindingDocumentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender is not RichTextBox control || e.NewValue is not FlowDocument flowDocument)
return;
control.Document = flowDocument;
}
/// <summary>
/// A callback function that is executed when the value of the WordWrapping is changed.
/// </summary>
/// <param name="sender">The object that issued the event</param>
/// <param name="e">Event parameter.</param>
private static void WordWrappingChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender is BindableRichTextBox bindableRichTextBox)
{
ResizeDocument(bindableRichTextBox);
}
}
#endregion
/// <summary>
/// Resizes the width of BindableRichTextBox according to the value of WordWrapping.
/// </summary>
/// <param name="control">BindableRichTextBox object</param>
private static void ResizeDocument(BindableRichTextBox control)
{
if (control.WordWrapping)
{
control.Document.PageWidth = double.NaN;
}
else
{
control.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
var size = MeasureString(control);
control.Document.PageWidth = size.Width + 15;
}
}
/// <summary>
/// Calculate text size on RichTextBox.
/// </summary>
/// <param name="control">RichTextBox object</param>
/// <returns>Size of text on RichTextBox.</returns>
private static System.Windows.Size MeasureString(BindableRichTextBox control)
{
var text = new TextRange(control.Document.ContentStart, control.Document.ContentEnd).Text;
var formattedText = new FormattedText(text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(control.FontFamily, control.FontStyle, control.FontWeight, control.FontStretch),
control.FontSize,
System.Windows.Media.Brushes.Black,
control._dpiX);
return new System.Windows.Size(formattedText.Width, formattedText.Height);
}
public BindableRichTextBox()
{
using var graphics = Graphics.FromHwnd(IntPtr.Zero);
_dpiX = graphics.DpiX;
TextChanged += (_, _) => ResizeDocument(this);
}
}
}