Come ottenere RTF da RichTextBox
-
03-07-2019 - |
Domanda
Come posso ottenere il testo in RTF di un RichTextBox
? Sto cercando di ottenere in questo modo, ma la proprietà non esiste.
RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;
Soluzione
Per ottenere l'XAML effettivo creato dall'utente all'interno di RichTextBox:
TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
tr.Save(ms, DataFormats.Xaml);
string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
EDIT: non ho un codice davanti a me da testare, ma un'istanza del tipo TextRange
ha un metodo Salva
(in streaming) che accetta un DataFormats
, che può essere DataFormats.Rtf
Altri suggerimenti
Esistono 2 classi RichTextBox, una dal framework winforms e una dal framework WPF:
System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;
Solo il RichTextBox Winforms ha una proprietà Rtf, l'altro ha una proprietà Document che contiene un FlowDocument.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow