Favicon

Data binding to RichTextBox

Peponi1/21/20252m

C#
SystemWindowsFormsRichTextBoxBinding

1. Introduction

Control의 data binding을 위해 다음 중 하나가 필요하다.

  • INotifyPropertyChanged 구현 : XAML 바인딩을 위해 사용하는 것과 동일하다.
  • 바인딩 대상 객체의 형식에 프로퍼티 변경 이벤트 구현

여기서는 INotifyPropertyChanged 인터페이스를 통한 바인딩 방법을 알아본다.

2. Example

using System.ComponentModel;
using System.Runtime.CompilerServices;
 
namespace RichTextBoxDataBinding
{
    public partial class Form1 : Form, INotifyPropertyChanged
    {
        // INotifyPropertyChanged 구현
        public event PropertyChangedEventHandler? PropertyChanged;
 
        private string _textData = string.Empty;
 
        public string TextData
        {
            get => _textData;
            set
            {
                _textData = value;
                OnPropertyChanged();    // 프로퍼티가 변경되었음을 알림
            }
        }
 
        private const string _sampleRtf = "{\\rtf1\\ansi\\ansicpg949\\deff0\\nouicompat\\deflang1033\\deflangfe1042{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n{\\colortbl ;\\red0\\green77\\blue187;\\red0\\green0\\blue255;}\r\n{\\*\\generator Riched20 10.0.19041}\\viewkind4\\uc1 \r\n\\pard\\sa200\\sl276\\slmult1\\fs20\\lang18 Hello, World!\\par\r\n\\cf1 Hello, World!\\cf0\\par\r\n\\i Hello, World!\\par\r\n\\b\\i0 Hello, World!\\par\r\n{\\i{\\field{\\*\\fldinst{HYPERLINK https://google.com }}{\\fldrslt{https://google.com\\ul0\\cf0}}}}\\f0\\fs20\\par\r\n}";
 
        public Form1()
        {
            InitializeComponent();
 
            ConfigureComponents();
        }
 
        private void ConfigureComponents()
        {
            RichTextBox textBox = new()
            {
                Dock = DockStyle.Fill,
                Height = 300
            };
            TextBox checkInput = new()      // 텍스트 확인용
            {
                ReadOnly = true,
                Multiline = true,
                WordWrap = true,
                Dock = DockStyle.Fill,
                ScrollBars = ScrollBars.Vertical
            };
            Button button = new() { Text = "Sample" };
            button.Click += delegate { TextData = _sampleRtf; };        // 샘플 RTF 입력
 
            // RichTextBox 입력 후 포커스 이동해야 TextData가 업데이트됨
            // textBox.DataBindings.Add(new Binding(nameof(textBox.Text), this, nameof(TextData)));  // 일반 텍스트
            textBox.DataBindings.Add(new Binding(nameof(textBox.Rtf), this, nameof(TextData)));      // RTF 텍스트
            checkInput.DataBindings.Add(new Binding(nameof(checkInput.Text), this, nameof(TextData)));
 
            TableLayoutPanel tableLayoutPanel = new()
            {
                ColumnCount = 2,
                RowCount = 2,
                Dock = DockStyle.Fill
            };
            tableLayoutPanel.ColumnStyles.Add(new(SizeType.Percent, 80));
            tableLayoutPanel.ColumnStyles.Add(new(SizeType.Percent, 20));
            tableLayoutPanel.Controls.Add(textBox, 0, 0);
            tableLayoutPanel.Controls.Add(checkInput, 0, 1);
            tableLayoutPanel.Controls.Add(button, 1, 0);
 
            this.Controls.Add(tableLayoutPanel);
        }
 
        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

IMPORTANT

RTF에 대한 자세한 내용은 Rich Text Format (RTF) Specification, version 1.6을 참조한다.

3. 참조 자료