The RichTextArea Control

This new Silverlight 4 only control allows you to input RichText into your Silverlight application.  Here is a simple example of its use, complete with some simple formatting commands.

We have created an enhanced version of the RichTextArea control which enables you to Load and Save the content as basic HTML.  This control is available to download with all the source code here.

<UserControl x:Class="RichTextArea.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White" Width="300" Height="300">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border Grid.Row="0" BorderThickness="1" BorderBrush="#8b919f">
            <StackPanel Orientation="Horizontal">
                <StackPanel.Background>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Color="#ffffff" Offset="0.0" />
                        <GradientStop Color="#e3e8f4" Offset="0.5" />
                        <GradientStop Color="#cfd7eb" Offset="0.5" />
                        <GradientStop Color="#e9ecfa" Offset="1.0" />
                    </LinearGradientBrush>
                </StackPanel.Background>
                <StackPanel Orientation="Horizontal" Margin="4">
                    <ComboBox x:Name="selectFontFamily" Width="155" Height="23" Margin="0 0 2 0" SelectionChanged="SelectFontFamily_ItemSelected">
                        <ComboBoxItem Content="Arial" FontSize="14" FontFamily="Arial" IsSelected="True" />
                        <ComboBoxItem Content="Arial Black" FontSize="14" FontFamily="Arial Black" />
                        <ComboBoxItem Content="Comic Sans MS" FontSize="14" FontFamily="Comic Sans MS" />
                        <ComboBoxItem Content="Courier New" FontSize="14" FontFamily="Courier New" />
                        <ComboBoxItem Content="Lucida Sans Unicode" FontSize="14" FontFamily="Lucida Sans Unicode" />
                        <ComboBoxItem Content="Times New Roman" FontSize="14" FontFamily="Times New Roman" />
                        <ComboBoxItem Content="Trebuchet MS" FontSize="14" FontFamily="Trebuchet MS" />
                        <ComboBoxItem Content="Verdana" FontSize="14" FontFamily="Verdana" />
                    </ComboBox>
                    <ComboBox x:Name="selectFontSize" Width="45" Height="23" Margin="0, 0, 10, 0" SelectionChanged="SelectFontSize_ItemSelected">
                        <ComboBoxItem Content="6" />
                        <ComboBoxItem Content="7" />
                        <ComboBoxItem Content="8" />
                        <ComboBoxItem Content="9" />
                        <ComboBoxItem Content="10" />
                        <ComboBoxItem Content="11" />
                        <ComboBoxItem Content="12" IsSelected="True" />
                        <ComboBoxItem Content="13" />
                        <ComboBoxItem Content="14" />
                        <ComboBoxItem Content="15" />
                        <ComboBoxItem Content="16" />
                        <ComboBoxItem Content="17" />
                        <ComboBoxItem Content="18" />
                        <ComboBoxItem Content="20" />
                        <ComboBoxItem Content="22" />
                        <ComboBoxItem Content="24" />
                        <ComboBoxItem Content="26" />
                        <ComboBoxItem Content="28" />
                        <ComboBoxItem Content="36" />
                        <ComboBoxItem Content="48" />
                        <ComboBoxItem Content="72" />
                    </ComboBox>
                    <Button x:Name="makeBold" Width="24" Height="23" Margin="0 0 2 0" Click="MakeBold_Click">
                        <TextBlock x:Name="boldText" Text="B" FontFamily="Arial" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Button>
                    <Button x:Name="makeItalic" Width="24" Height="23" Margin="0 0 2 0" Click="MakeItalic_Click">
                        <TextBlock x:Name="italicText" Text="I" FontFamily="Arial" FontSize="14" FontStyle="Italic" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Button>
                    <Button x:Name="makeUnderline" Width="24" Height="23" Margin="0 0 10 0" Click="MakeUnderline_Click">
                        <TextBlock x:Name="underlineText" Text="U" FontFamily="Arial" FontSize="14" TextDecorations="Underline" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Button>
                </StackPanel>
            </StackPanel>
        </Border>
        <RichTextArea x:Name="rta" Grid.Row="1" />
    </Grid>
</UserControl>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace RichTextArea
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void SelectFontFamily_ItemSelected(object sender, EventArgs e)
        {
            if (selectFontFamily != null)
            {
                rta.Selection.SetPropertyValue(TextElement.FontFamilyProperty, new FontFamily(((ComboBoxItem)selectFontFamily.SelectedItem).FontFamily.Source));
            }
        }

        private void SelectFontSize_ItemSelected(object sender, EventArgs e)
        {
            if (selectFontSize != null)
            {
                rta.Selection.SetPropertyValue(TextElement.FontSizeProperty, double.Parse(((ComboBoxItem)selectFontSize.SelectedItem).Content.ToString()));
            }
        }

        private void MakeBold_Click(object sender, RoutedEventArgs e)
        {
            rta.Selection.SetPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
        }

        private void MakeItalic_Click(object sender, RoutedEventArgs e)
        {
            rta.Selection.SetPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
        }

        private void MakeUnderline_Click(object sender, RoutedEventArgs e)
        {
            rta.Selection.SetPropertyValue(TextElement.TextDecorationsProperty, TextDecorations.Underline);
        }
    }
}


Here we show how to format text using the basic formatting commands such as Bold, Italic and Underline as well as Font Family and Size.

tmdavis83 wrote:

I have a string of text that is actually a RTF document.  I can't seem to get it to display as one.  It only displays as the raw text of the RTF document.  How do i change that?

dharmbhav wrote:

Woopie, sorry for my delayed response.


I did the following:


public BaseFloatingWindow(IRegionManager regionManager, IEventAggregator eventAggregator)

{

this.regionManager = regionManager;

InitializeComponent();

RegionManager.SetRegionManager(this, regionManager);

this.eventAggregator = eventAggregator;


closeWindowEvent = eventAggregator.GetEvent<CloseWindowEvent>();

closeWindowEvent.Subscribe(SubscribeToCloseEvent, true);

this.Closed+=new EventHandler(BaseFloatingWindow_Closed);

}

public void BaseFloatingWindow_Closed(object sender, EventArgs e)

{

foreach (object view in this.regionManager.Regions[regionName].Views)

{

if (((UserControl)view).DataContext is ViewModelBase)

{

((ViewModelBase)((UserControl)view).DataContext).KeepAlive = false;

}

regionManager.Regions[regionName].Remove(view);

}

Dispose();

}

private void SubscribeToCloseEvent(object obj)

{

if (regionName == obj.ToString())

{

closeWindowEvent.Unsubscribe(SubscribeToCloseEvent);

this.Close();

BaseFloatingWindow_Closed(new object(), new EventArgs());

}

}


Hope this helps.

Woopie wrote:

I found the bug in the latest version too.

@dharmbhav hopefully you get this but can you advise on the binding for MVVM please...

Thanks for the good library

dharmbhav wrote:

There seems to be an issue with v 5.3.3 of RichTextEditor. I wanted to use it in my Prism based MVVM application (with a little bit tweaking for Databinding) and it threw an object reference null exception. With v 5.2.7 it works fine now.

Sadly you havn't uploaded source code for v5.2.7 controls.

A good library though,

Thanks

hussam wrote:

How to Databind RichTextEditor?

paweb wrote:

How to implement superscript in RichTextBox but in web application ?

Post your Comments

tmdavis83 wrote:

I have a string of text that is actually a RTF document.  I can't seem to get it to display as one.  It only displays as the raw text of the RTF document.  How do i change that?

dharmbhav wrote:

Woopie, sorry for my delayed response.


I did the following:


public BaseFloatingWindow(IRegionManager regionManager, IEventAggregator eventAggregator)

{

this.regionManager = regionManager;

InitializeComponent();

RegionManager.SetRegionManager(this, regionManager);

this.eventAggregator = eventAggregator;


closeWindowEvent = eventAggregator.GetEvent<CloseWindowEvent>();

closeWindowEvent.Subscribe(SubscribeToCloseEvent, true);

this.Closed+=new EventHandler(BaseFloatingWindow_Closed);

}

public void BaseFloatingWindow_Closed(object sender, EventArgs e)

{

foreach (object view in this.regionManager.Regions[regionName].Views)

{

if (((UserControl)view).DataContext is ViewModelBase)

{

((ViewModelBase)((UserControl)view).DataContext).KeepAlive = false;

}

regionManager.Regions[regionName].Remove(view);

}

Dispose();

}

private void SubscribeToCloseEvent(object obj)

{

if (regionName == obj.ToString())

{

closeWindowEvent.Unsubscribe(SubscribeToCloseEvent);

this.Close();

BaseFloatingWindow_Closed(new object(), new EventArgs());

}

}


Hope this helps.

Woopie wrote:

I found the bug in the latest version too.

@dharmbhav hopefully you get this but can you advise on the binding for MVVM please...

Thanks for the good library

dharmbhav wrote:

There seems to be an issue with v 5.3.3 of RichTextEditor. I wanted to use it in my Prism based MVVM application (with a little bit tweaking for Databinding) and it threw an object reference null exception. With v 5.2.7 it works fine now.

Sadly you havn't uploaded source code for v5.2.7 controls.

A good library though,

Thanks

hussam wrote:

How to Databind RichTextEditor?

paweb wrote:

How to implement superscript in RichTextBox but in web application ?

Post your Comments

 
Latest Games
Zombie Escape
Apr 19, 2016
Plays: 1,404

Zombie Escape ScreenshotDrive fast before the crazy mutant zombies get you!

6 Ratings/4.1 Average
Car Parking
Jan 16, 2016
Plays: 1,371

Car Parking ScreenshotGuide the car to its parking space in this fun Car Parking game.

1 Rating/5 Average
Trash It
Jan 11, 2016
Plays: 1,257

Trash It ScreenshotAim for the Trash Can and get the various items of Trash in the bin.

4 Ratings/5 Average
Sky Fly
Jan 11, 2016
Plays: 1,377

Sky Fly ScreenshotFly your plane in this colorful vertical scrolling shoot-em-up. Blast the bad guys and collect any bonus's they leave behind.

1 Rating/5 Average
Professor Snappy
Jan 11, 2016
Plays: 1,010

Professor Snappy ScreenshotPop as many bubbles as possible in this fun and colorful bubble popping game. The levels start off easy enough but gradually get harder!

1 Rating/5 Average
Monster Match Saga
Jan 10, 2016
Plays: 1,342

Monster Match Saga ScreenshotHere we have a bunch of monsters that need to be matched up. Look out for the bomb and spinning monsters that will cause special damage!

3 Ratings/4.6 Average
Fly Bird Fly
Jan 10, 2016
Plays: 1,063

Fly Bird Fly ScreenshotGuide your friendly Bird through the maze of pipes and other obstacles collecting the Stars in this cool arcade game inspired by the legendary Flappy Bird.

1 Rating/5 Average
Life In One
Jan 10, 2016
Plays: 1,258

Life In One ScreenshotYou are stranded on an Alien planet. Your goal is to build a space rocket and escape. Start by building units to create power and mine the metal patches. Build defenses to defend your base from the advancing Aliens and Zombies!

2 Ratings/3 Average
X Pool
Jan 02, 2016
Plays: 1,761

X Pool ScreenshotPlay Pool against the computer or battle against your friends in the online mode!

3 Ratings/3 Average
Fruit Slicer
Jan 02, 2016
Plays: 1,022

Fruit Slicer ScreenshotSlice the fruit that is thrown up onto the screen. Slice the fruit into multiple pieces for maximum points!

1 Rating/5 Average