【发布时间】:2016-09-27 01:21:05
【问题描述】:
我有一个来自数据库的 RTF 格式的字符串。字符串如下:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI, Lucida Sans Unicode, Verdana;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch This is a sentence}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}
我有以下作为 DataGrid XAML 代码:
<DataGrid Name="dg">
<DataGrid.Columns>
<DataGridTextColumn Header="Col1" Binding="{Binding Col1}"/>
<DataGridTextColumn Header="Col1" Binding="{Binding Col1}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<RichTextBox>
<FlowDocument>
<Paragraph>
<Run Text="{Binding Text}" />
</Paragraph>
</FlowDocument>
</RichTextBox>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
我遇到的问题是来自数据库的值正在显示,但它是 RTF 格式。
我不确定如何转换它,以便 RichTextBox 显示适当的文本格式。
我有以下方法将数据加载到 RichTextBox 中,但问题是我需要先知道 RichTextBox 的名称。
public static string convertString_RTF(string text, RichTextBox rtb)
{
string rtfText = text;
byte[] byteArray = Encoding.ASCII.GetBytes(rtfText);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray))
{
System.Windows.Documents.TextRange tr = new System.Windows.Documents.TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
tr.Load(ms, System.Windows.DataFormats.Rtf);
}
return null;
}
如何通过绑定在 RichTextBox 中显示 RTF 字符串?
提前感谢您的帮助。
【问题讨论】:
-
两个都是很好的答案,非常感谢您花时间帮助我解决这个问题。
-
第二个答案是错误的。猜猜为什么 ?仔细考虑后会告诉你的。
标签: wpf datagrid richtextbox datatemplate rtf