Xaml 为richtextbox中的FlowDocument将文本格式从字符串应用到文本
我知道,在XAML中添加文本/内容/数据上下文时,您需要参考资源字典或内联标记,以便围绕文本或模板进行样式设置 Q: 但是,我很难找到一种方法来执行以下操作:Xaml 为richtextbox中的FlowDocument将文本格式从字符串应用到文本,xaml,Xaml,我知道,在XAML中添加文本/内容/数据上下文时,您需要参考资源字典或内联标记,以便围绕文本或模板进行样式设置 Q: 但是,我很难找到一种方法来执行以下操作: <local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center" VerticalAlignment="Center"/> 数据来自从数据库中提取的视图模型 (字符串值) 我是个
<local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
数据来自从数据库中提取的视图模型
(字符串值)
我是个聪明人。
要在流文档中显示,请执行以下操作:
我是个聪明人
Q端
通过绑定到转换器、行为,或者将我放在流文档中的段落/文档保存到内存流中的.rtf文件是更好的选择吗
我已尝试使用列出的行为><的选项,但这是针对文本块的,无法重定向为键入文本而不是文本块
试图使其流线型化
尝试使用数据绑定并应用转换器,但尽管我有用于行为/转换器的资源,但由于类型转换,它仍能工作。Rockford Lhotka在文章中提出了一个聪明的解决方案。他的想法是创建一个自定义控件,然后使用创建RichTextBlock 这允许您使用如下代码:
<local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
你好在哪里
public string Hello { get; set; } = "I am a <Bold>smart</Bold> man.";
publicstring Hello{get;set;}=“我是个聪明人。”;
结果是:
如果您使用UWP/Win 8.1 XAML,您可以使用博客文章中的原始代码,并进行以下小改动(添加段落):
");
附加(ctl.xaml);
xaml.Append(@)
");
回答我自己的问题:
我的案例是创建一个文档样式的显示,供用户更新并另存为PDF,但我不想依赖Office作为我们的应用服务器
因此,在我的例子中,我通过使用完整的“doc.RTF”文档并将其作为内存流/字符串导入,并对其应用所需的值更新来解决这个问题
i、 e.VB.net代码段示例
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
然后,我将带有该内存流的XAML富文本框加载为DataFormats.Rtf
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代码布局,但是如果您发现它有用,您可以从中进行简化和调整
谢谢上述方案不能作为XamlReader使用。加载采用未分配的Stream、XamlReader或XmlReader。