如何在DataGrid的DataTemplate中的RichTextBox中将转换字符串转换为RTF格式?

时间:2016-09-27 01:21:06

标签: wpf datagrid richtextbox datatemplate rtf

我有一个来自数据库的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字符串的解决方案是什么?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

因为您需要使用RichTextBox.Load,所以无法直接绑定。使用这样的转换器:

    [ValueConversion(typeof(string), typeof(string))]
    public class RTFConverter : IValueConverter
    {
        RichTextBox rtBox = new RichTextBox();
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string rtf = (string)value;
            convertString_RTF(rtf, rtBox);
            TextRange textRange = new TextRange(rtBox.Document.ContentStart, rtBox.Document.ContentEnd);
            return textRange.Text;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

<Window.Resources>
   <local:RTFConverter x:Key="RTFConverter"/>
</Window.Resources>
<RichTextBox x:Name="richtxt" VerticalAlignment="Bottom" >
    <FlowDocument>
        <Paragraph>
            <Run Text="{Binding Text, Converter={StaticResource RTFConverter}}" />
        </Paragraph>
    </FlowDocument>
</RichTextBox>

答案 1 :(得分:1)

  1. 创建自己的RichTextBox,并引入名为DependencyProperty的新TextProperty

    public class MyRichTextBox : RichTextBox
    {
        public string Text
        {
          get { return (string)GetValue(TextProperty); }
          set { SetValue(TextProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
               DependencyProperty.Register("Text", typeof(string), typeof(MyRichTextBox), new PropertyMetadata(String.Empty, new PropertyChangedCallback(TextChanged_Handler)));
    
        private static void TextChanged_Handler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RichTextBox rtb = d as RichTextBox;
    
            byte[] array = Encoding.ASCII.GetBytes(e.NewValue.ToString());
            using (MemoryStream stream = new MemoryStream(array))
            {
                TextRange t = new TextRange(rtb.Document.ContentStart,
                                rtb.Document.ContentEnd);
                t.Load(stream, System.Windows.DataFormats.Rtf);
            }                
         }       
    }
    
  2. 用法:

    <local:MyRichTextBox Margin="20" Text="{Binding RtfCol}" Width="300" Height="300" />
    
    1. 您也可以选择AttachedProperty
    2. 的路线