简体   繁体   English

如何在WPF中使用RichTextBox

[英]How to work with RichTextBox in WPF

I have a WPF application in which I want to convert a TextBox into a RichTextBox. 我有一个WPF应用程序,我想在其中将TextBox转换为RichTextBox。 I have already written the following lines of code: 我已经编写了以下代码行:

            <RichTextBox>
                <FlowDocument>
                    <Paragraph>
                        <Run Text="{Binding GeneralDescription}" />
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>

This has the effect that the string GeneralDescription is displayed and I can edit and format it. 这样可以显示字符串GeneralDescription,并且我可以对其进行编辑和格式化。 Now I have the problem that when I mark a part of the text, format it (eg make it bold), save the document and re-open the document, only the part of the text until the formatting is displayed. 现在,我遇到一个问题,当我标记文本的一部分时,将其格式化(例如,使其变粗),保存文档并重新打开文档,仅显示文本的一部分,直到显示格式。 I am not sure if the error lies within the display or within the saving. 我不确定错误是在显示内还是在保存内。 In either case it's annoying. 无论哪种情况,这都很烦人。 How can I make it work? 我该如何运作? Is it a problem that GeneralDescription is of type string? GeneralDescription是字符串类型的问题吗?

Thanks in advance. 提前致谢。

The Problem is that your string data contains only plain text and your XAML design tags in it will be ignored at loading. 问题是您的string数据仅包含纯文本,并且其中的XAML design tags在加载时将被忽略。


Textbox and RichTextBox are controls with a complete different behavior. TextboxRichTextBox是具有完全不同行为的控件。

The problem also is that RichTextBox don't support this kind of binding native. 问题还在于RichTextBox不支持这种本地绑定。 A Document on a RichTextBox is not a dependency property, that's why. RichTextBox上的Document不是依赖项属性,这就是原因。

Personally i use David Veeneman extended control for cases like this. 就我个人而言,我使用David Veeneman扩展控制。


For Saving or Loading a FlowDocument directly use: 要直接保存或加载FlowDocument ,请使用:

FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
XamlWriter.Save(myFlowDocumentObject, fs);

and

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FlowDocument myFlowDocumentObject = XamlReader.Load(fs) as FlowDocument;

By the way, the Run Tag data binding is partially supported. 顺便说一下,部分支持Run标签数据绑定。

  • One way data binding is fully supported. 完全支持数据绑定的一种方式。 A Run can be bound to a data source and the content of the Run will reflect the value of what it is bound to. 可以将运行绑定到数据源,并且运行的内容将反映其绑定的值。 The bound Run will receive and display any changes that occur in the data source. 绑定的“运行”将接收并显示数据源中发生的任何更改。

  • Two way data binding is partially supported . 部分支持两种方式的数据绑定 If a bound Run is updated via calls to the WPF property system, the data source which the Run is bound to will reflect the changes to the Run. 如果通过调用WPF属性系统更新绑定的运行,则运行所绑定到的数据源将反映对运行的更改。 On the other hand, if a bound Run is updated via a RichTextBox or the text object model, the Run will lose its binding . 另一方面, 如果通过RichTextBox或文本对象模型更新了绑定的Run则Run将失去其binding

我可以使用此页面上提供的代码解决问题: http : //www.codeproject.com/Articles/37169/WPF-RichText-Editor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM