如何從WPF的富文本框中讀取段落並將其顯示在消息框中?C# - 從富文本框中讀取段落
-1
A
回答
2
如果你想通過所有的段落在RichTextBox
迭代,然後含extension methods以下靜態類提供必要的方法:
public static class FlowDocumentExtensions
{
public static IEnumerable<Paragraph> Paragraphs(this FlowDocument doc)
{
return doc.Descendants().OfType<Paragraph>();
}
}
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;
}
}
一旦你收集到的所有段落在FlowDocument
,以轉換單款文本,你可以這樣做:
var text = new TextRange(paragraph.ContentStart, paragraph.ContentEnd).Text;
和實例如何把這些結合在一起是:
foreach (var paragraph in canvas.Document.Paragraphs())
{
MessageBox.Show(new TextRange(paragraph.ContentStart, paragraph.ContentEnd).Text);
}
這就是你想要的嗎?
更新
如果因任何原因,你使用擴展方法是不舒服,你總是可以使用傳統的C#2.0的靜態方法:
public static class FlowDocumentExtensions
{
public static IEnumerable<Paragraph> Paragraphs(FlowDocument doc)
{
return DependencyObjectExtensions.Descendants(doc).OfType<Paragraph>();
}
}
public static class DependencyObjectExtensions
{
public static IEnumerable<DependencyObject> Descendants(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;
}
}
而且
foreach (var paragraph in FlowDocumentExtensions.Paragraphs(mainRTB.Document))
{
MessageBox.Show(new TextRange(paragraph.ContentStart, paragraph.ContentEnd).Text);
}
相關問題
- 1. 從富文本框中讀取文本行
- 2. 從文本文件中讀取特定段落matlab
- 3. 僅在富文本框中的段落中的第二行縮進(c#)
- 4. 從Python的文本文件中讀取某些段落?
- 5. objective c iOS,從文本框中讀取?
- 6. 從文本中提取段落perl
- 7. C#WPF富文本框BackgroundProperty從文件
- 8. 從Excel單元格讀取富文本
- 9. 從c#中的富文本框中取代一行#
- 10. 從Silverlight富文本框中提取純文本 - 從LINQ到XML
- 11. C#富文本框高亮
- 12. C#富文本框樣式
- 13. 單獨讀取文本文件中的每個段落
- 14. 如何從富文本框
- 15. 從富文本框中獲取所選文本
- 16. 文本框中的Python docx段落
- 17. Flash Builder只讀富文本字段?
- 18. C#中的靜態富文本框#
- 19. 將文本與豐富的UITextView中的段落與CoreText對齊?
- 20. 拖放到只讀富文本框
- 21. 從文本文件中讀取隨機文本到C中的文本框#
- 22. 如何從非創建的文本框中讀取文本框?
- 23. C++。從文本文件中讀取。每個第二段丟失
- 24. 從插件的富文本框中獲取選定的文字
- 25. 從C#中的文本文件讀取#
- 26. 從文本文件中讀取。 C++
- 27. 從文本文件中讀取objective-c
- 28. 從文件中讀取文本c
- 29. C從文本文件中讀取值
- 30. 從C中的文本文件讀取
的TextRange的TextRange =新TextRange(canvas.Document.ContentStart,canvas.Document.ContentEnd); MessageBox.Show(textRange.Text);但它給了我箱中的所有文字 – GunJack
把這個放在你的問題 –