How do you get the selected text of a WPF FlowDocument?

霸气de小男生 提交于 2020-01-14 18:50:33

问题


I'm using .NET 3.5

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.

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...


回答1:


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);

hope this helps, regards



来源:https://stackoverflow.com/questions/2024886/how-do-you-get-the-selected-text-of-a-wpf-flowdocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!