代码:
TextPointer navigator = flowDocViewer.Document.ContentStart;
// While we are not at end of document
while (navigator.CompareTo(flowDocViewer.Document.ContentEnd) < 0)
{
// Get text pointer context
TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
Run run = navigator.Parent as Run;
if (run != null)
{
if (run.Text.Equals(currentText))
{
if (run.IsFocused == false)
{
run.GotFocus += run_GotFocus;
run.LostFocus += run_LostFocus;
run.Focus();
}
}
}
和:
void run_LostFocus(object sender, RoutedEventArgs e)
{
((Run)sender).LostFocus -= run_LostFocus;
((Run)sender).Background = Brushes.Transparent;
}
void run_GotFocus(object sender, RoutedEventArgs e)
{
((Run)sender).GotFocus -= run_GotFocus;
((Run)sender).Background = Brushes.White;
}