【问题标题】:How to load RTF Text from database and display it in data template in a Datagrid? WPF C#如何从数据库加载 RTF 文本并将其显示在 Datagrid 的数据模板中? WPF C#
【发布时间】:2021-06-27 17:42:35
【问题描述】:

我正在尝试从数据库加载 RTF 文本并将其显示到自定义 RichTextBox 的数据模板中。 我想获得带有样式的文本,代码很简单,但是当我尝试使用 <GridViewDataColumn.CellTemplate> <GridViewDataColumn.CellTemplate/> 时,它变得很困难

  • Xaml 代码:

      <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#代码:

          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;
      }
    

如何在数据模板中显示来自 RTF 内容的文本?

【问题讨论】:

  • 什么是UC_Descriptiondocument 属性返回什么?您的示例代码没有多大意义。
  • 我通过 RichTextBox 在数据库中注册了数据,我想将这些内容加载到 datgridview 单元格中
  • 你没有回答我的问题。您在单元格中放置了UC_Description。为什么?这是什么?您要绑定的document 是什么?
  • UC_Description是一个自定义RichtextBox的用户控件,数据库中存在的文档是RTF类型的。
  • 在实际创建之前,您无法获得对 RichTextBox 的引用。看我的回答。

标签: c# wpf telerik richtextbox radgridview


【解决方案1】:

模板就是模板,在运行时应用之前没有UC_DescriptionRichTextBox

您可以做的是处理UserControlLoaded 事件,然后将RichTextBoxDocument 属性设置为您的文档:

XAML:

<telerik:GridViewDataColumn.CellTemplate>
    <DataTemplate >
        <local:UC_Description Loaded="OnLoaded" />
    </DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>

代码:

private void OnLoaded(object sender, RoutedEventArgs e)
{
    UC_Description uc = (UC_Description)sender;
    uc.richTextBox.Document = ...;
}

【讨论】:

  • 对不起,事件处理程序的名称是OnLoaded。它是您在模板的 XAML 标记中连接的那个。我编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 2011-10-22
相关资源
最近更新 更多