@ -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." ) ;
}
}
}
}
}
}