Click here to Skip to main content
15,891,787 members
Articles / Desktop Programming / WPF

A Bindable WPF RichTextBox

Rate me:
Please Sign up or sign in to vote.
4.99/5 (48 votes)
13 Aug 2010CPOL14 min read 262.1K   12.6K   88  
An article about a Bindable WPF RichTextBox
using System;
using System.Windows.Input;

namespace FsRichTextBoxDemo
{
    public class LoadDocumentCommand : ICommand
    {
        #region Fields

        // Member variables
        private MainWindowViewModel m_ViewModel;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public LoadDocumentCommand(MainWindowViewModel viewModel)
        {
            m_ViewModel = viewModel;
        }

        #endregion

        #region ICommand Members

        /// <summary>
        /// Whether the LoadDocumentCommand is enabled.
        /// </summary>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Actions to take when CanExecute() changes.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Executes the LoadDocumentCommand
        /// </summary>
        public void Execute(object parameter)
        {
            /* We set the Document property on the view model to simulate 
             * a document load from the app back-end. */

            m_ViewModel.DocumentXaml = "<FlowDocument PagePadding=\"5,0,5,0\" AllowDrop=\"True\" " 
                + "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
                + "<Paragraph>Text generated by app back-end</Paragraph></FlowDocument>";
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions