dimanche 28 juin 2015

WPF RichTextBox CaretPosition

I'm trying to program a simple WordPad (not Notepad) in WPF. And at the very beginning, I ran into a problem.

  1. Write a text
  2. Place caret in the middle of text
  3. Change text size
  4. The cursor jumps to the end of the run

        public MainWindow()
    {
        InitializeComponent();
        fontSizeComBox.ItemsSource = new List() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
    }

    private void fontSizeComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Run r = new Run();
        Paragraph p = new Paragraph();
        r.FontSize = (double)fontSizeComBox.SelectedValue;
        richTextBox.CaretPosition.Paragraph.Inlines.Add(r);
        richTextBox.CaretPosition = r.ElementStart;
        richTextBox.Focus();
    }

Aucun commentaire:

Enregistrer un commentaire