From d37d90a255127f12f01327b72bc8ea2a9f5a5773 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Fri, 4 Feb 2011 03:35:28 +0000 Subject: [PATCH] RichTextBox: fixed bug when using custom formatter and binding to null/empty text property. Also added RichTextBox.Clear method to allow user to programmatically clear the contents of the RTB. --- .../RichTextBox/Formatters/RtfFormatter.cs | 22 ++++++++++++++++--- .../RichTextBox/Formatters/XamlFormatter.cs | 17 ++++++++++---- .../RichTextBox/RichTextBox.cs | 8 +++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs index e5a1def6..9b2b895c 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs @@ -23,10 +23,26 @@ namespace Microsoft.Windows.Controls public void SetText(FlowDocument document, string text) { - TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); - using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) + try + { + //if the text is null/empty clear the contents of the RTB. If you were to pass a null/empty string + //to the TextRange.Load method an exception would occur. + if (String.IsNullOrEmpty(text)) + { + document.Blocks.Clear(); + } + else + { + TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); + using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) + { + tr.Load(ms, DataFormats.Rtf); + } + } + } + catch { - tr.Load(ms, DataFormats.Rtf); + throw new InvalidDataException("Data provided is not in the correct RTF format."); } } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs index 07e1d31a..250f391b 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs @@ -25,15 +25,24 @@ namespace Microsoft.Windows.Controls { try { - TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); - using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) + //if the text is null/empty clear the contents of the RTB. If you were to pass a null/empty string + //to the TextRange.Load method an exception would occur. + if (String.IsNullOrEmpty(text)) { - tr.Load(ms, DataFormats.Xaml); + document.Blocks.Clear(); + } + else + { + TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); + using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) + { + tr.Load(ms, DataFormats.Xaml); + } } } catch { - throw new InvalidDataException("data provided is not in the correct Xaml format."); + throw new InvalidDataException("Data provided is not in the correct Xaml format."); } } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs index bc2240a7..bb4f57a7 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs @@ -95,6 +95,14 @@ namespace Microsoft.Windows.Controls _textSetInternally = false; } + /// + /// Clears the content of the RichTextBox. + /// + public void Clear() + { + Document.Blocks.Clear(); + } + #endregion //Methods #region Event Hanlders