如何获取WPF FlowDocument的所选文本? [英] How do you get the selected text of a WPF FlowDocument?

查看:803
本文介绍了如何获取WPF FlowDocument的所选文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET 3.5

I'm using .NET 3.5

我在FlowDocumentScrollViewer中有一个FlowDocument。我不使用RichTextBox。 FlowDocument附带了一些赠品,包括文本选择和用于复制/粘贴的上下文菜单。

I have a FlowDocument inside a FlowDocumentScrollViewer. I am not using RichTextBox. The FlowDocument comes with a number of "freebies", including text selection and a context menu for copy/paste.

如何找到当前在FlowDocument?我想我可以使用ApplicationCommands.Copy获取文本到剪贴板,然后从那里读取,但我不想更改剪贴板的内容,如果我不必。

How can I find out what text is currently selected in the FlowDocument? I imagine that I could use ApplicationCommands.Copy to get the text into the clipboard and then read it from there, but I don't want to change the contents of the clipboard if I don't have to.

必须有我遗漏的东西...

There must be something I'm missing...

推荐答案

什么版本的.net框架是你使用吗?从版本3.5开始,为FlowDocumentScrollViewer控件引入了Selection属性。您可以使用它来处理选定的文本,例如:

What version of .net framework are you using? Since version 3.5 there is Selection property introduced for FlowDocumentScrollViewer control. You can use it to work with selected text, smth like this:

TextPointer potStart = flowDocumentScrollViewer.Selection.Start;
TextPointer potEnd = flowDocumentScrollViewer.Selection.End;
TextRange range = new TextRange(potStart,potEnd);
Console.WriteLine(range.Text);

希望这有助于您,

这篇关于如何获取WPF FlowDocument的所选文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆