<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);
}
}
}