RichTextBox for UWP | ComponentOne
Working with C1Document Object / Using the C1Document Class
In This Topic
    Using the C1Document Class
    In This Topic

    As discussed earlier, the C1RichTextBox provides a linear, flat view of the control content, while C1Document exposes the document structure.

    To illustrate the advantages of working directly with the document object, suppose you wanted to add some functionality to the previous sample: when the user presses the CTRL key, you want to capitalize the text in all paragraphs of type Heading2.

    The object model exposed by the C1RichTextBox is not powerful enough to do this reliably. You would have to locate spans based on their formatting, which would be inefficient and unreliable (what if the user formatted some plain text with the same format used by Heading2?).

    Using the C1Document object model, this task becomes trivial. You simply have to handle the KeyDown event within the InitializeComponent() method:

    C#
    Copy Code
    public MainPage()
            {
                this.InitializeComponent();
             
                                    //No changes here...
    
             // Bind the second C1RichTextBox to the same document
             rtb2.Document = _rtb.Document;
             rtb2.KeyDown += rtb2_KeyDown;
            }
       
            void rtb2_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == VirtualKey.Control)
                {
                    var h2 = _rtb.Document.Blocks.OfType<Heading2>().FirstOrDefault();
                    if (h2 != null)
                        h2.ContentRange.ToUppercase();
                }
            }
    

    The code monitors the keyboard. When the user presses the CTRL key, it enumerates all Heading2 elements in the document and replaces their content with capitals.