简体   繁体   中英

IncreaseIndentation with WPF/c# RichTextBox

I'm getting some unexpected behavior when attempting to insert bullets in the WPF RichTextBox using the EditCommands Interface. All I want to do is programmatically type "hello1", toggle the bullets to on, go down one line, type "hello2" with bullets still enabled, then increate the indent by one.

What I see:

在此处输入图像描述

What I want to See:

在此处输入图像描述

XML:

<Window x:Class="WpfApp3.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <RichTextBox Name="richtextbox1"/>
    </Grid>
</Window>

C#:

using System.Windows;
using System.Windows.Documents;

namespace WpfApp3 {    
    public partial class MainWindow : Window {
        public MainWindow() { InitializeComponent(); }
        private void RboxInsert(string text) {
            richtextbox1.CaretPosition.InsertTextInRun(text);
            for(int i=0; i < text.Length; i++)
                EditingCommands.MoveRightByCharacter.Execute(null, richtextbox1);
        }
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            richtextbox1.Focus();
            RboxInsert("Hello1");
            EditingCommands.ToggleBullets.Execute(null, richtextbox1);
            EditingCommands.EnterParagraphBreak.Execute(null, richtextbox1);
            RboxInsert("Hello2");
            EditingCommands.IncreaseIndentation.Execute(null, richtextbox1);
            EditingCommands.EnterParagraphBreak.Execute(null, richtextbox1);
            return;
        }
    }
}

Second Attempt:

   public partial class MainWindow : Window {
        public MainWindow() { InitializeComponent(); }
        private void RboxInsert(string text) {
            richtextbox1.CaretPosition.InsertTextInRun(text);
            for(int i=0; i < text.Length; i++)
                EditingCommands.MoveRightByCharacter.Execute(null, richtextbox1);
        }
        private void Window_Loaded(object sender, RoutedEventArgs e) {
            richtextbox1.Focus();
            RboxInsert("Hello1");
            EditingCommands.ToggleBullets.Execute(null, richtextbox1);
            EditingCommands.EnterParagraphBreak.Execute(null, richtextbox1);
            richtextbox1.Selection.ApplyPropertyValue(
                Paragraph.TextIndentProperty,
                (double)100 /*pixels to indent by*/
            );
            RboxInsert("Hello2");
            return;
        }
    }

Result of Second Attempt:

在此处输入图像描述

在此处输入图像描述

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

namespace WpfApp5
{
    internal class RBox
    {
        private RichTextBox rbox;
        public RBox(RichTextBox rbox)
        {
            this.rbox = rbox;
        }
        public void Document_NoSpacing()
        {
            // Nospaces between Paragraphs
            const double margin = 10.0;
            rbox.SelectAll();
            rbox.Selection.ApplyPropertyValue(
                Paragraph.MarginProperty,
                new Thickness(left: margin, top: 0, right: margin, bottom: 0)
            );
        }

        private double indent_pixel_step = 20; /*pixels*/

        public int ParagraphIndentGetByLevel()
        {
            var para = rbox.CaretPosition.Paragraph;
            var margin = para.Margin;
            if (double.IsNaN(margin.Left))
                margin.Left = 0;
            double pixels = margin.Left;
            return (int)Math.Ceiling(pixels / indent_pixel_step);
        }

        public void ParagraphIndentByLevel(int level)
        {
            var para = rbox.CaretPosition.Paragraph;
            var margin = para.Margin;
            margin.Left = indent_pixel_step * level;
            para.Margin = margin;
            return;
        }


        public void InputString(string text)
        {
            rbox.CaretPosition.InsertTextInRun(text);
            for (int i = 0; i < text.Length; i++)
                EditingCommands.MoveRightByCharacter.Execute(null, rbox);
        }

        public void InputCtrlEnter()
        {
            rbox.CaretPosition = rbox.CaretPosition.InsertLineBreak();
        }

        public void InputEnter()
        {
            rbox.CaretPosition = rbox.CaretPosition.InsertParagraphBreak();
        }

        public void BulletInsert(string text, int level, string bullet)
        {
            InputEnter();
            InputString($"{bullet} {text}");
            ParagraphIndentByLevel(level);
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var longline = ""; // new string('-', 200);
            var rbox = new RBox(richtextbox1);
            rbox.Document_NoSpacing();
            rbox.BulletInsert($"hello1 {longline}", 0, "*");
            rbox.BulletInsert($"hello2 {longline}", 1, "*");
            rbox.BulletInsert($"hello3 {longline}", 2, "*");
            rbox.BulletInsert($"hello2 {longline}", 1, "*");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM