WPF RichTextBox TextChangedEvent - Finding Nearest Paragraph Start
Using WPF in .Net 4, I have created a small WPF RichTextBox inside a window. I have successfully been able to catch the TextChangedEvent event for the RichTextBox, and I can traverse through the changes array provided by the TextChangedEventArgs.
I'm using the code found here: Best way to imp开发者_StackOverflowlement a Parsing/Editable Richtextbox in WPF
Well, say I type the letter 'a' into the RichTextBox at runtime, and have a breakpoint during the TextChangedEvent in order to inspect the contents of the RichTextBox using the debugger. I want to then perform the following:
string sa = range.Start.Paragraph.ContentStart.GetTextInRun(LogicalDirection.Forward);
But the problem is that 'sa' is always an empty string no matter what I put into the RichTextBox at runtime. Shouldn't 'sa' be a string starting at the beginning of the paragraph relative to a given change? Or should I use something else besides range.Start.Paragraph.ContentStart
?
Just figured it out... I had to create a new TextRange using ContentStart and ContentEnd of the Paragraph, like this:
var range2 = new TextRange(range.Start.Paragraph.ContentStart, range.Start.Paragraph.ContentEnd);
精彩评论