Richtextbox wpf 绑定
要在 WPF RichtextBox
中对 Document
进行数据绑定,到目前为止我看到了 2 个解决方案,它们是从 RichtextBox
派生并添加 < code>DependencyProperty,以及带有“代理”的解决方案。
第一个和第二个都不令人满意。有人知道另一种解决方案,或者是一个能够数据绑定的商业 RTF 控件吗?普通的 TextBox
不是替代方案,因为我们需要文本格式。
有什么想法吗?
To do DataBinding of the Document
in a WPF RichtextBox
, I saw 2 solutions so far, which are to derive from the RichtextBox
and add a DependencyProperty
, and also the solution with a "proxy".
Neither the first or the second are satisfactory. Does somebody know another solution, or instead, a commercial RTF control which is capable of DataBinding? The normal TextBox
is not an alternative, since we need text formatting.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有一个更简单的方法!
您可以轻松创建附加的
DocumentXaml
(或DocumentRTF
)属性,该属性允许您绑定RichTextBox
的文档。它的使用方式如下,其中Autobiography
是数据模型中的字符串属性:瞧!完全可绑定
RichTextBox
数据!该属性的实现非常简单:设置该属性后,将 XAML(或 RTF)加载到新的 FlowDocument 中。当
FlowDocument
更改时,更新属性值。此代码应该可以解决问题:
相同的代码可用于 TextFormats.RTF 或 TextFormats.XamlPackage。对于 XamlPackage,您将拥有
byte[]
类型的属性,而不是string
。与普通 XAML 相比,XamlPackage 格式具有多个优点,特别是能够包含图像等资源,并且比 RTF 更灵活且更易于使用。
很难相信这个问题已经搁置了 15 个月,却没有人指出解决这个问题的简单方法。
There is a much easier way!
You can easily create an attached
DocumentXaml
(orDocumentRTF
) property which will allow you to bind theRichTextBox
's document. It is used like this, whereAutobiography
is a string property in your data model:Voila! Fully bindable
RichTextBox
data!The implementation of this property is quite simple: When the property is set, load the XAML (or RTF) into a new
FlowDocument
. When theFlowDocument
changes, update the property value.This code should do the trick:
The same code could be used for TextFormats.RTF or TextFormats.XamlPackage. For XamlPackage you would have a property of type
byte[]
instead ofstring
.The XamlPackage format has several advantages over plain XAML, especially the ability to include resources such as images, and it is more flexible and easier to work with than RTF.
It is hard to believe this question sat for 15 months without anyone pointing out the easy way to do this.
我知道这是一篇旧帖子,但请查看扩展 WPF 工具包。它有一个 RichTextBox 支持您尝试执行的操作。
I know this is an old post, but check out the Extended WPF Toolkit. It has a RichTextBox that supports what you are tryign to do.
我对之前的代码做了一些调整。
首先 range.Changed 对我不起作用。
在我将 range.Changed 更改为 richTextBox.TextChanged 后,事实证明 TextChanged 事件处理程序可以递归调用 SetDocumentXaml,因此我提供了针对它的保护。我还使用 XamlReader/XamlWriter 而不是 TextRange。
I have tuned up previous code a little bit.
First of all range.Changed hasn't work for me.
After I changed range.Changed to richTextBox.TextChanged it turns out that TextChanged event handler can invoke SetDocumentXaml recursively, so I've provided protection against it. I also used XamlReader/XamlWriter instead of TextRange.
我可以给你一个不错的解决方案,你可以接受它,但在我这样做之前,我将尝试解释为什么 Document 不是
DependencyProperty
。在
RichTextBox
控件的生命周期内,Document
属性通常不会更改。RichTextBox
使用FlowDocument
进行初始化。该文档已显示,可以通过多种方式进行编辑和修改,但Document
属性的基础值仍然是FlowDocument
的一个实例。因此,实际上没有理由它应该是一个 DependencyProperty,即 Bindable。如果您有多个位置引用此FlowDocument
,则只需引用一次。由于到处都是相同的实例,因此每个人都可以访问这些更改。我认为
FlowDocument
不支持文档更改通知,尽管我不确定。话虽这么说,这是一个解决方案。在开始之前,由于
RichTextBox
未实现INotifyPropertyChanged
并且 Document 不是DependencyProperty
,因此当RichTextBox< /code> 的 Document 属性发生变化,因此绑定只能是 OneWay。
创建一个提供
FlowDocument
的类。绑定需要存在DependencyProperty
,因此此类继承自DependencyObject
。使用 XAML 创建一个带有富文本框的
Window
。为
Window
提供一个HasDocument
类型的字段。窗口构造函数应该创建绑定。
如果您希望能够在 XAML 中声明绑定,则必须使
HasDocument
类从FrameworkElement
派生,以便可以将其插入到逻辑树中。现在,如果您要更改
HasDocument
上的Document
属性,富文本框的Document
也会更改。I can give you an ok solution and you can go with it, but before I do I'm going to try to explain why Document is not a
DependencyProperty
to begin with.During the lifetime of a
RichTextBox
control, theDocument
property generally doesn't change. TheRichTextBox
is initialized with aFlowDocument
. That document is displayed, can be edited and mangled in many ways, but the underlying value of theDocument
property remains that one instance of theFlowDocument
. Therefore, there is really no reason it should be aDependencyProperty
, ie, Bindable. If you have multiple locations that reference thisFlowDocument
, you only need the reference once. Since it is the same instance everywhere, the changes will be accessible to everyone.I don't think
FlowDocument
supports document change notifications, though I am not sure.That being said, here's a solution. Before you start, since
RichTextBox
doesn't implementINotifyPropertyChanged
and Document is not aDependencyProperty
, we have no notifications when theRichTextBox
's Document property changes, so the binding can only be OneWay.Create a class that will provide the
FlowDocument
. Binding requires the existence of aDependencyProperty
, so this class inherits fromDependencyObject
.Create a
Window
with a rich text box in XAML.Give the
Window
a field of typeHasDocument
.Window constructor should create the binding.
If you want to be able to declare the binding in XAML, you would have to make your
HasDocument
class derive fromFrameworkElement
so that it can be inserted into the logical tree.Now, if you were to change the
Document
property onHasDocument
, the rich text box'sDocument
will also change.这似乎是迄今为止最简单的方法,并且没有显示在任何这些答案中。
在视图模型中只有
Text
变量。This seems to be the easiest way by far and isn't displayed in any of these answers.
In the view model just have the
Text
variable.创建一个 UserControl,其中有一个名为 RTB 的 RichTextBox。现在添加以下依赖属性:
此解决方案可能是您在某处看到的“代理”解决方案..但是.. RichTextBox 根本没有 Document 作为 DependencyProperty... 所以您必须以另一种方式执行此操作...
HTH
Create a UserControl which has a RichTextBox named RTB. Now add the following dependency property:
This solution is probably that "proxy" solution you saw somewhere.. However.. RichTextBox simply does not have Document as DependencyProperty... So you have to do this in another way...
HTH
https://stackoverflow.com/a/2989277/3001007的答案满足了我的大部分需求="https://stackoverflow.com/users/360374/krzysztof">krzysztof。但该代码的一个问题(我面临的是),绑定不适用于多个控件。因此,我使用基于
Guid
的实现更改了_recursionProtection
。所以它也适用于同一窗口中的多个控件。为了完整起见,让我在原始答案中添加几行 https://stackoverflow.com/a/2641774/3001007作者:ray-burns。这就是如何使用助手。
Most of my needs were satisfied by this answer https://stackoverflow.com/a/2989277/3001007 by krzysztof. But one issue with that code (i faced was), the binding won't work with multiple controls. So I changed
_recursionProtection
with aGuid
based implementation. So it's working for Multiple controls in same window as well.For completeness sake, let me add few more lines from original answer https://stackoverflow.com/a/2641774/3001007 by ray-burns. This is how to use the helper.
这是我的解决方案,基于 Ray Burns 的答案,使用 DataBinding 并将 XAML 字符串转换为 RichTextBox 文档:
ViewModel
View
RichTextBoxHelper
Here is my solution based on Ray Burns answer with DataBinding and conversion of a XAML-string to a RichTextBox-Document:
ViewModel
View
RichTextBoxHelper
这是 Lolo 答案的 VB.Net 版本:
End Class
Here's a VB.Net version of Lolo's answer:
End Class
这个 VB.Net 版本适合我的情况。我删除了线程集合信号量,而是使用RemoveHandler 和AddHandler。另外,由于 FlowDocument 一次只能绑定到一个 RichTextBox,因此我检查了 RichTextBox 的 IsLoaded=True。让我们从我如何在 MVVM 应用程序中使用该类开始,该应用程序使用 ResourceDictionary 而不是 Window。
This VB.Net version works for my situation. I removed thread collection semaphore, instead using RemoveHandler and AddHandler. Also, since a FlowDocument can only be bound to one RichTextBox at a time, I put in a check that the RichTextBox's IsLoaded=True. Let's begin with how I used the class in a MVVM app which uses ResourceDictionary instead of Window.
伙计们,为什么要为所有这些事情烦恼呢?这非常有效。无需代码
Guys why bother with all the faff. This works perfectly. No code required