Port source generation test harness

I haven't figured out how to make the AppendText(text, color) extension
work right with the WPF version of RichTextBox, but I was able to work
around the issue, and it's not really worth sinking time into.
This commit is contained in:
Andy McFadden committed 2019-07-06 14:20:51 -07:00
1 parent 9aa8b5c9a7
commit 83fe70535b
9 files changed
+1033 -1

No files matched your search

+28
View File
@@ -18,6 +18,7 @@ using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
@@ -217,4 +218,31 @@ namespace CommonWPF {
}
#endif
}
/// <summary>
/// RichTextBox extensions.
/// </summary>
public static class RichTextBoxExtensions {
/// <summary>
/// Overloads RichTextBox.AppendText() with a version that takes a color as an argument.
/// NOTE: color is "sticky", and will affect the next call to the built-in AppendText()
/// method.
/// </summary>
/// <remarks>
/// Adapted from https://stackoverflow.com/a/23402165/294248
///
/// TODO(someday): figure out how to reset the color for future calls.
/// </remarks>
public static void AppendText(this RichTextBox box, string text, Color color) {
TextRange tr = new TextRange(box.Document.ContentEnd, box.Document.ContentEnd);
tr.Text = text;
try {
tr.ApplyPropertyValue(TextElement.ForegroundProperty,
new SolidColorBrush(color));
} catch (FormatException ex) {
Debug.WriteLine("RTB AppendText extension failed: " + ex);
}
}
}
}