【问题标题】:apply text formatting from string to text for FlowDocument in richtextbox将文本格式从字符串应用到 Richtextbox 中 FlowDocument 的文本
【发布时间】:2017-08-17 15:39:50
【问题描述】:

我知道在 XAML 中添加 text/content/DataContext 时,您指的是资源字典或内联标记,用于围绕文本或模板进行样式设置。

问: 但是,我在尝试找到一种方法来执行以下操作时遇到了麻烦:

数据来自从数据库中提取的视图模型/模型。

(字符串值) I am a <Bold>smart</Bold> man.

在流文档中显示如下:

我是一个聪明的人

Q 结束

通过绑定到转换器、行为,或者将我放入流文档中的段落/文档保存到内存流中的 .rtf 文件是更好的选择?

我已尝试将选项用于列出的行为 > here

尽量精简。

尝试使用数据绑定并应用转换器,但即使我有行为/转换器的资源,由于类型转换,它仍然有效。

【问题讨论】:

    标签: xaml


    【解决方案1】:

    Rockford Lhotka 在帖子Set rich text into RichTextBlock control 中提出了一个巧妙的解决方案。他的想法是创建一个自定义控件,然后使用XamlReader.Load 创建 RichTextBlock。

    这允许您使用如下代码:

        <local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center" 
                               VerticalAlignment="Center"/>
    

    你好在哪里:

        public string Hello { get; set; } = "I am a <Bold>smart</Bold> man.";
    

    结果:

    如果您使用 UWP/Win 8.1 XAML,您可以使用博客文章中的原始代码,并进行以下小改动(添加的段落):

    <UserControl 
      xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
      xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" 
      xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006""> 
      <Grid> 
        <RichTextBlock><Paragraph>");
                xaml.Append(ctl.Xaml);
                xaml.Append(@" 
        </Paragraph></RichTextBlock> 
      </Grid> 
    </UserControl> 
    ");
    

    【讨论】:

    • 上述建议不起作用,因为 XamlReader.Load 采用未分配的 Stream、XamlReader 或 XmlReader。
    【解决方案2】:

    回答我自己的问题: 我的案例是创建一个文档样式显示供用户更新并保存为 PDF,但我不想依赖 Office 在我们的应用程序服务器上。

    因此,我通过使用完整的“doc.RTF”文档并将其作为内存流/字符串导入并应用我需要的值更新来解决这个问题。

    即VB.net sn-p 示例

    Using uStream = Assembly.GetExecutingAssembly.GetManifestResourceStream("Resourcefilepath.rtf") 
        Using mStream As system.IO.MemoeryStream = New MemoryStream()
            uStream.CopyTo(mStream)
            rtfstring = Encoding.UTF8.GetSTring(mStream.toArray())
            '--Do the updates to the needed string as needed:
            rtfstring.Replace("Value","UpdatedValue")
            '--Load Property Memory String this method is returnind  
            RTFDataProperty = New MemoryStream(Encoding.UTF8.GetBytes(rtfstring))
        End Using
    End Using
    

    然后我将该内存流作为 DataFormats.Rtf 加载到我的 XAML 富文本框。

    RichTextBox1.SelectAll()
    RichTextBox1.Selection.Load(ClassName.RTFDataProperty, DataFormats.Rtf)
    

    这给了我一个用于格式化和布局该文档的模板。 (更多的是案例场景,而不是正常做法)

    我还想应用一个起始选择,所以这就是我在那里所做的:

    '--Get my RichTextBox Text
    rtbtext As String = New TextRange(RichTextBox1.Document.contentStart, RichTextbox1.Document.ContentEnd).Text
    Dim strStartSelection As String = "Comments..."
    Dim startTP As TextPointer
    Dim endTP As TextPointer
    
    '--Loop through the paragraphs of the richtextbox for my needed selection starting point:
    For Each para As Paragraph In RichTextBox1.Document.Blocks
        Dim paraText As String = New TextRange(para.ContentStart, para.ContentEnd).Text
        If paraText = "" Then
            Dim pos As TextPointer = para.ContentStart
            startTP = pos
            endTP = startTP.GetPositionAtOffset("".Length + 3) '--my string had ... on the end so had to add for avoiding the escape of that on length
            RichTextBox1.Selection.Select(startTP, endTP)
            RichTextBox1.Focus()
            Exit For
        End If
    Next
    

    这是简单的 VB.net 代码布局,但如果您觉得有用,可以从那里简化和调整。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 2014-09-17
      • 2010-11-30
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2011-06-21
      相关资源
      最近更新 更多