Richtextbox wpf 绑定 [英] Richtextbox wpf binding

查看:79
本文介绍了Richtextbox wpf 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在 WPF RichtextBox 中对 Document 进行数据绑定,到目前为止我看到了 2 个解决方案,它们是从 RichtextBox 派生的,并且添加DependencyProperty,以及带有代理"的解决方案.

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".

第一个和第二个都不令人满意.有人知道另一种解决方案,或者是能够DataBinding 的商业 RTF 控件吗?普通的 TextBox 不是替代品,因为我们需要文本格式.

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.

有什么想法吗?

推荐答案

有一个更简单的方法!

您可以轻松创建附加的 DocumentXaml(或 DocumentRTF)属性,该属性将允许您绑定 RichTextBox 的文档.它是这样使用的,其中 Autobiography 是数据模型中的字符串属性:

You can easily create an attached DocumentXaml (or DocumentRTF) property which will allow you to bind the RichTextBox's document. It is used like this, where Autobiography is a string property in your data model:

<TextBox Text="{Binding FirstName}" />
<TextBox Text="{Binding LastName}" />
<RichTextBox local:RichTextBoxHelper.DocumentXaml="{Binding Autobiography}" />

瞧!完全可绑定的 RichTextBox 数据!

Voila! Fully bindable RichTextBox data!

该属性的实现非常简单:设置该属性后,将 XAML(或 RTF)加载到新的 FlowDocument 中.当 FlowDocument 更改时,更新属性值.

The implementation of this property is quite simple: When the property is set, load the XAML (or RTF) into a new FlowDocument. When the FlowDocument changes, update the property value.

这段代码应该可以解决问题:

This code should do the trick:

using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
public class RichTextBoxHelper : DependencyObject
{
    public static string GetDocumentXaml(DependencyObject obj)
    {
        return (string)obj.GetValue(DocumentXamlProperty);
    }

    public static void SetDocumentXaml(DependencyObject obj, string value)
    {
        obj.SetValue(DocumentXamlProperty, value);
    }

    public static readonly DependencyProperty DocumentXamlProperty =
        DependencyProperty.RegisterAttached(
            "DocumentXaml",
            typeof(string),
            typeof(RichTextBoxHelper),
            new FrameworkPropertyMetadata
            {
                BindsTwoWayByDefault = true,
                PropertyChangedCallback = (obj, e) =>
                {
                    var richTextBox = (RichTextBox)obj;

                    // Parse the XAML to a document (or use XamlReader.Parse())
                    var xaml = GetDocumentXaml(richTextBox);
                    var doc = new FlowDocument();
                    var range = new TextRange(doc.ContentStart, doc.ContentEnd);

                    range.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)),
                          DataFormats.Xaml);

                    // Set the document
                    richTextBox.Document = doc;

                    // When the document changes update the source
                    range.Changed += (obj2, e2) =>
                    {
                        if (richTextBox.Document == doc)
                        {
                            MemoryStream buffer = new MemoryStream();
                            range.Save(buffer, DataFormats.Xaml);
                            SetDocumentXaml(richTextBox,
                                Encoding.UTF8.GetString(buffer.ToArray()));
                        }
                    };
                }
            });
}

相同的代码可用于 TextFormats.RTF 或 TextFormats.XamlPackage.对于 XamlPackage,您将拥有 byte[] 类型的属性,而不是 string.

The same code could be used for TextFormats.RTF or TextFormats.XamlPackage. For XamlPackage you would have a property of type byte[] instead of string.

与普通 XAML 相比,XamlPackage 格式有几个优点,尤其是能够包含图像等资源,并且比 RTF 更灵活、更易于使用.

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.

很难相信这个问题持续了 15 个月,却没有人指出解决这个问题的简单方法.

It is hard to believe this question sat for 15 months without anyone pointing out the easy way to do this.

这篇关于Richtextbox wpf 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆