[英]IncreaseIndentation with WPF/c# RichTextBox
I'm getting some unexpected behavior when attempting to insert bullets in the WPF RichTextBox using the EditCommands Interface.尝试使用 EditCommands 接口在 WPF RichTextBox 中插入项目符号时出现一些意外行为。 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.
我要做的就是以编程方式键入“hello1”,将项目符号切换为打开,go 向下一行,键入“hello2”,项目符号仍然启用,然后将缩进增加一个。
What I see:我所看到的:
What I want to See:我想看的:
XML: 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#: 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, "*");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.