65.9K
CodeProject 正在变化。 阅读更多。
Home

可以绑定到 RTF 属性的 RichTextBox

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.87/5 (14投票s)

2010 年 6 月 24 日

CPOL

3分钟阅读

viewsIcon

69194

downloadIcon

1653

终于,一个可以绑定到 RTF 属性的 RichTextBox

引言

此控件允许用户快速轻松地将对象数据绑定到 RichTextBox 控件的 Rtf 属性。

背景

在工作中,我领导一个团队,负责将我们的 Microsoft Access 2003 应用程序转换为具有 SQL 2005 后端数据库的 .NET 应用程序。我们的程序在太多地方使用 RTF 格式化的字符串... 发票、时间卡、备注、约会、文档备注、项目任务等。

在 Microsoft Access 中,我们使用了一个名为 Total Access Memo 的第三方 ActiveX 库,它对于我们想要做的事情来说非常棒。但在 .NET 中,Microsoft 很好地给了我们 RichTextBox 控件。唯一的问题是他们没有让我们能够轻松地将数据绑定到控件的 Rtf 属性!

因此,在使用一种完全黑客式的、绕弯的方法绑定数据后,我问自己“不能绑定到富文本的 RichTextBox 有什么意义?” 这个问题困扰了我,以至于我不得不修复它。

Using the Code

这个控件可以像默认的 RichTextBox 一样使用。唯一的区别是您现在在设计器的 PropertyGrid 中的“(Databindings)”类别下有一个名为 Rtf 的附加属性。

dbRTBox 类继承了 System.Windows.Forms.RichTextBox 类,并用一个新的类隐藏了 Rtf 属性。

代码非常简单

C#

using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DataboundRTB
{
    [ClassInterface(ClassInterfaceType.AutoDispatch), DefaultBindingProperty("Rtf"), 
	Description("DescriptionRichTextBox"), ComVisible(true), 
	Docking(DockingBehavior.Ask), 
	Designer("System.Windows.Forms.Design.RichTextBoxDesigner, 
	System.Design, Version=4.0.0.0, Culture=neutral, 
	PublicKeyToken=b03f5f7f11d50a3a")]
    class dbRTBox : System.Windows.Forms.RichTextBox
    {
        [Bindable(true), RefreshProperties(RefreshProperties.All), 
	SettingsBindable(true), DefaultValue(""), Category("Appearance")]
        new public string Rtf
        {
            get
            {
                return base.Rtf;
            }
            set
            {
                base.Rtf = value;
            }
        }
    }
} 

VB

Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDispatch), _
	DefaultBindingProperty("Rtf"), Description("DescriptionRichTextBox"), _
	ComVisible(True), Docking(DockingBehavior.Ask), _
	Designer("System.Windows.Forms.Design.RichTextBoxDesigner, _
	System.Design, Version=4.0.0.0, Culture=neutral, _
	PublicKeyToken=b03f5f7f11d50a3a")> _
Public Class dbRTBox
    Inherits RichTextBox

    <Bindable(True), RefreshProperties(RefreshProperties.All), _
	SettingsBindable(True), DefaultValue(""), Category("Appearance")> _
    Public Overloads Property Rtf As String
        Get
            Return MyBase.Rtf
        End Get
        Set(ByVal value As String)
            MyBase.Rtf = value
        End Set
    End Property

End Class 

正如您所见,该类有一个属性 (Rtf) 隐藏了 base.Rtf 属性。我们这样做只是为了将标签放在属性上方,以便让设计器知道我们希望允许在此属性上进行数据绑定。

代码...

Bindable(true)

... 是使这一切工作的关键代码行。此标签告诉设计器您要绑定到此属性。

由于我们几乎总是希望绑定到 Rtf 属性,因此我们还希望向类添加一个标签...

C#

 [ClassInterface(ClassInterfaceType.AutoDispatch), 
	DefaultBindingProperty("Rtf"), Description("DescriptionRichTextBox"), 
	ComVisible(true), Docking(DockingBehavior.Ask), 
	Designer("System.Windows.Forms.Design.RichTextBoxDesigner, 
	System.Design, Version=4.0.0.0, Culture=neutral, 
	PublicKeyToken=b03f5f7f11d50a3a")]    

VB

 <ClassInterface(ClassInterfaceType.AutoDispatch), 
	DefaultBindingProperty("Rtf"), Description("DescriptionRichTextBox"), 
	ComVisible(True), Docking(DockingBehavior.Ask), 
	Designer("System.Windows.Forms.Design.RichTextBoxDesigner, 
	System.Design, Version=4.0.0.0, Culture=neutral, 
	PublicKeyToken=b03f5f7f11d50a3a")> _ 

在此标签中,我们利用默认的 RichTextBox 标签,但我们添加了标签 DefaultBindingProperty("Rtf"),因此当我们从 DataSources 窗口将字段拖到 dbRTBox 控件上时,它将直接绑定到 Rtf 属性,而不是 Text 属性。

在示例代码中,Form1 有一个名为 DataItem 的子类。

public class DataItem
{
    public DataItem(int i)
    {
        using (RichTextBox rtb = new RichTextBox())
        {
            rtb.Text = "Item #" + i.ToString();
            Key = rtb.Rtf;
        }
        Value = i;
    }

    public string Key { get; set; }
    public int Value { get; set; }
}  

在此类中,您可以在构造函数中看到我们只是创建了一个 RTF 格式的字符串(使用 RichTextbox :-D),其中只包含当前项的项目编号,并将其存储到 Key 属性中。

Form1 的构造函数只是将包含 20 个项目的 List<DataItem> 加载到 Form1_DataItemBindingSource 中。使用窗体上的“Next”和“Prev”按钮,您可以浏览绑定源中的项目,并看到实际项目已绑定到控件的 Rtf 属性,而不是 Text 属性。

关注点

此控件的最佳部分(除了它的简单性之外)是您可以从 WinForms 设计器中执行数据绑定,因此即使是最没有经验的程序员也可以只用鼠标在片刻之间轻松地对富文本字段进行数据绑定。

历史

  • 创建于 2010 年 6 月 24 日
  • 于 2010 年 6 月 24 日更新,以包含 VB 代码
© . All rights reserved.