Browse Source

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.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
d37d90a255
  1. 22
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs
  2. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs
  3. 8
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs

22
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs

@ -23,10 +23,26 @@ namespace Microsoft.Windows.Controls
public void SetText(FlowDocument document, string text) public void SetText(FlowDocument document, string text)
{ {
TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); try
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))
{
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.");
} }
} }
} }

17
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs

@ -25,15 +25,24 @@ namespace Microsoft.Windows.Controls
{ {
try try
{ {
TextRange tr = new TextRange(document.ContentStart, document.ContentEnd); //if the text is null/empty clear the contents of the RTB. If you were to pass a null/empty string
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) //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 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.");
} }
} }
} }

8
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs

@ -95,6 +95,14 @@ namespace Microsoft.Windows.Controls
_textSetInternally = false; _textSetInternally = false;
} }
/// <summary>
/// Clears the content of the RichTextBox.
/// </summary>
public void Clear()
{
Document.Blocks.Clear();
}
#endregion //Methods #endregion //Methods
#region Event Hanlders #region Event Hanlders

Loading…
Cancel
Save