Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 48.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RichTextBoxExtensions.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Extension methods for <see cref="RichTextBox" />.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Windows.Controls;
using System.Windows.Documents;
using Catel.Windows.Documents;

namespace Catel.Windows.Controls
{
    /// <summary>
    /// Extension methods for <see cref="RichTextBox"/>.
    /// </summary>
    public static class RichTextBoxExtensions
    {
		/// <summary>
		/// Sets the text of a <see cref="RichTextBox"/>. This method will replace
		/// any existing content.
		/// </summary>
		/// <param name="rtb"><see cref="RichTextBox"/> that should contain the text.</param>
		/// <param name="text">Text to set.</param>
        public static void SetText(this RichTextBox rtb, string text)
        {
            var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
			textRange.ApplyPropertyValue(Control.FontSizeProperty, rtb.Document.FontSize);
			textRange.ApplyPropertyValue(Control.FontFamilyProperty, rtb.Document.FontFamily);

            if (text.GetDataFormat() == System.Windows.DataFormats.Text)
            {
                text = text.ConvertPlainTextToRtf(rtb.FontSize, rtb.FontFamily);
            }

            textRange.SetText(text);
        }

        /// <summary>
        /// Returns the text that a <see cref="RichTextBox"/> contains.
        /// </summary>
        /// <param name="rtb"><see cref="RichTextBox"/> that contains the text.</param>
        /// <returns>String containing the text of the <see cref="RichTextBox"/> control.</returns>
        public static string GetText(this RichTextBox rtb)
        {
            var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
            return textRange.GetText();
        }
    }
}

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
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions