Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Get text from RichtextBox content non-visually (DataFormats.XamlPackage)

$
0
0

Hi,

 

I have the need to obtain the raw text value from a RichtextBox, but I do not have a visual object to use.  I have the content of the RTB in DataFormats.XamlPackage (not .Xaml so I can support images), and if I use a visual component I (FlowDocument, TextRange etc) I can reconstitute the RTB and get the raw Text from it (or the textRange).

 

However, I was hoping to do this non-visually using the DatObject class, as so:

 

DataObjectxaml=newDataObject(DataFormats.XamlPackage, text);

text=xaml.GetText();

 

I always seem to get an empty string.

 

The follwing works:

TextRangetr=newTextRange(Document.ContentStart, Document.ContentEnd);

MemoryStreammsOut=newMemoryStream(ASCIIEncoding.Default.GetBytes(text));

msOut.Seek(0, System.IO.SeekOrigin.Begin);

tr.Load(msOut, DataFormats.XamlPackage);

 

assuming 'this' is a RichTextBox and I can objtain the Text property from the TextRange tr.

 

The following similar code results in an exception:

FlowDocumentfd=newFlowDocument();

TextRangetr=newTextRange(fd.ContentStart, fd.ContentEnd);

MemoryStreammsOut=newMemoryStream(ASCIIEncoding.Default.GetBytes(text));

msOut.Seek(0, System.IO.SeekOrigin.Begin);

tr.Load(msOut, DataFormats.XamlPackage);

 

the last line generates an "Unable to read beyond the end of the stream" exception.

 

Does anyone have an idea how I can get the text in this situation?

 

Cheers,

 

Radek


Viewing all articles
Browse latest Browse all 18858

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>