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 16 years ago
parent
commit
d37d90a255
  1. 16
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/RtfFormatter.cs
  2. 11
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/Formatters/XamlFormatter.cs
  3. 8
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/RichTextBox/RichTextBox.cs

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

@ -22,6 +22,16 @@ namespace Microsoft.Windows.Controls
} }
public void SetText(FlowDocument document, string text) public void SetText(FlowDocument document, string 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); TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
@ -30,4 +40,10 @@ namespace Microsoft.Windows.Controls
} }
} }
} }
catch
{
throw new InvalidDataException("Data provided is not in the correct RTF format.");
}
}
}
} }

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

@ -24,6 +24,14 @@ namespace Microsoft.Windows.Controls
public void SetText(System.Windows.Documents.FlowDocument document, string text) public void SetText(System.Windows.Documents.FlowDocument document, string text)
{ {
try 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); TextRange tr = new TextRange(document.ContentStart, document.ContentEnd);
using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text))) using (MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(text)))
@ -31,9 +39,10 @@ namespace Microsoft.Windows.Controls
tr.Load(ms, DataFormats.Xaml); 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