Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to make a simple wordpad application. But it still doesn't work as I expected. Can anyone try the code below and tell me what I do wrong?

Thanks for advices.

XML
<Window x:Class="WPFRtBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="447" Width="395">
    <Grid>
        <ComboBox x:Name="fontSizeComBox" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="50" SelectionChanged="fontSizeComBox_SelectionChanged"/>
        <RichTextBox x:Name="rtBox" HorizontalAlignment="Left" Height="369" Margin="10,37,0,0" VerticalAlignment="Top" Width="367" SelectionChanged="rtBox_SelectionChanged">
            <RichTextBox.Resources>
                <Style TargetType="{x:Type Paragraph}">
                    <Setter Property="Margin" Value="0"/>
                </Style>
            </RichTextBox.Resources>
        </RichTextBox>
    </Grid>
</Window>


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFRtBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            fontSizeComBox.ItemsSource = new List<double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
            fontSizeComBox.SelectedIndex = 4;
            rtBox.AcceptsTab = true;
            rtBox.AcceptsReturn = true;
            rtBox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
        }

        private void fontSizeComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                Run r = new Run() { FontSize = (double)fontSizeComBox.SelectedValue };
                rtBox.CaretPosition.Paragraph.Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontSizeProperty, fontSizeComBox.SelectedItem);
            }
            rtBox.Focus();
        }

        private void rtBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            object selChanged = rtBox.Selection.GetPropertyValue(Inline.FontSizeProperty);
            fontSizeComBox.Text = selChanged.ToString();
        }
    }
}
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900