C# 如何从WPF RichTextBox获取所有编号列表
问题:从上面的WPF RichTextBox,我们如何以编程方式获得上面显示的列表 详细信息:使用下面的内容,我可以阅读上面RichTextBox中的所有段落。测试点击。。。下面显示的事件代码返回所有段落,如上图所示,每行都是一个段落。但是下面的代码没有告诉我哪个对象元素是: btnTest\u的输出单击。。。活动: MainWindow.xaml:C# 如何从WPF RichTextBox获取所有编号列表,c#,wpf,richtextbox,rtf,C#,Wpf,Richtextbox,Rtf,问题:从上面的WPF RichTextBox,我们如何以编程方式获得上面显示的列表 详细信息:使用下面的内容,我可以阅读上面RichTextBox中的所有段落。测试点击。。。下面显示的事件代码返回所有段落,如上图所示,每行都是一个段落。但是下面的代码没有告诉我哪个对象元素是: btnTest\u的输出单击。。。活动: MainWindow.xaml: 要枚举RichTextBox中的所有列表项,请从RichTextBox.Document.Blocks集合中选择所有System.Windows.
要枚举RichTextBox中的所有列表项,请从RichTextBox.Document.Blocks集合中选择所有System.Windows.Documents.list: MainWindow.xaml: C++ C V7.0 V8.0 Perl 标志 列举清单 MainWindow.xaml.cs: 使用System.Collections.Generic; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Documents; 公共部分类主窗口:窗口 { 公共主窗口 { 初始化组件; } 私有无效枚举列表对象发送方,RoutedEventArgs e { 变量列表=rtb.EnumerateList; 对于int i=0;i
List 1
1. C++
2. C#
a. v 7.0
b. v 8.0
List 2
1. Perl
2. Logo
注:
如果某个列表项具有子项SiblingListItems,例如在范围上方。文本将包含2。\tC\r\na。\tv 7.0\r\nb。\tv 8.0。这意味着所有同级列表项都包含在父项的文本中,但以\r\n分隔
如果有多个列表不一定嵌套子列表,如何获得这些列表的集合?我在官方MS文档中看到,但在列表集合中没有看到。@nam:我已更新了上面的代码,以枚举文档中的所有列表。EnumerateLists方法为RitchTextBox中的每个列表生成列表。非常有用,谢谢。小的打字错误,你可能指的是列表;i
.....
<RichTextBox x:Name="rtbTest" AcceptsTab="True" FontFamily="Calibri"/>
.....
using System.Windows.Documents;
using System.Linq;
namespace MyProjectName
{
public static class FlowDocumentExtensions
{
public static IEnumerable<Paragraph> Paragraphs(this FlowDocument doc)
{
return doc.Descendants().OfType<Paragraph>();
}
}
}
using System.Windows;
using System.Linq;
namespace MyProjectName
{
public static class DependencyObjectExtensions
{
public static IEnumerable<DependencyObject> Descendants(this DependencyObject root)
{
if (root == null)
yield break;
yield return root;
foreach (var child in LogicalTreeHelper.GetChildren(root).OfType<DependencyObject>())
foreach (var descendent in child.Descendants())
yield return descendent;
}
}
}
private void btnTest_Click(object sender, RoutedEventArgs e)
{
foreach (Paragraph paragraph in rtbTest.Document.Paragraphs())
{
System.System.Diagnostics.Debug.WriteLine(new TextRange(paragraph.ContentStart, paragraph.ContentEnd).Text);
}
}
List 1
1. C++
2. C#
a. v 7.0
b. v 8.0
List 2
1. Perl
2. Logo