I'm trying to load RTF text from database and display it into datatemplate of custom RichTextBox .
I would like to get the text with style , with code the matter is simple but when I'm trying to use <GridViewDataColumn.CellTemplate> <GridViewDataColumn.CellTemplate/> , it got difficult
Xaml code:
<telerik:GridViewDataColumn DataMemberBinding="{Binding document}" Width="*" x:Name="Description" IsReadOnly="True"> <telerik:GridViewDataColumn.CellTemplate> <DataTemplate > <local:UC_Description x:Name="richtext"> </local:UC_Description> </DataTemplate> </telerik:GridViewDataColumn.CellTemplate>C# code:
List= new Controller().GetAll(); foreach (Model item in List) { RtfFormatProvider provider = new RtfFormatProvider(); DocumentFormatProvidersManager.RegisterFormatProvider(provider); byte[] byteArray = Encoding.ASCII.GetBytes(item.Description); document = provider.Import(byteArray); FlowDocument flow = new FlowDocument(); } GridViewList.ItemsSource = null; GridViewList.ItemsSource = List; this.DataContext = this; } public RadDocument ImportXaml(string content) { RtfFormatProvider provider = new RtfFormatProvider(); return provider.Import(text); } public string RtfToPlainText(string rtf) { byte[] byteArray = Encoding.ASCII.GetBytes(rtf); var flowDocument = new FlowDocument(); TextRange tr; using (MemoryStream ms = new MemoryStream(byteArray)) { tr = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); tr.Load(ms, DataFormats.Rtf); } return tr.Text; }
How can I display text from RTF content in a data template?
A template is a template and there is no
UC_DescriptionorRichTextBoxuntil it has been applied at runtime.What you could do is to handle the
Loadedevent for theUserControland then set theDocumentproperty of theRichTextBoxto your document:XAML:
Code: