开发者

WPF FlowDocumentScrollViewer Selection.Select Not Working

I am trying to select text in my FlowDocumentScrollViewer.

I am able to find the TextPointer start positi开发者_如何学Goon and end position. So I have 2 TextPointers...

TextPointer startPos;
TextPointer endPos;

Using these 2 TextPointers I am trying to select text in my FlowDocumentScrollViewer. I am doing it like this...

flowDocumentScrollViewer.Selection.Select(startPos, endPos);

I would expect this to highlight the selected text. But it is not doing this.

Why is this not working???

[UPDATE] This is how I am getting the TextPointers:

TextPointer pointer = flowDocument.Document.ContentStart;
while (pointer != null)
{
  if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
  {
    string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
    // where textRun is the text in the flowDocument
    // and searchText is the text that is being searched for
    int indexInRun = textRun.IndexOf(searchText);
    if (indexInRun >= 0)
    {
      TextPointer startPos = pointer.GetPositionAtOffset(indexInRun);
      TextPointer endPos = pointer.GetPositionAtOffset(indexInRun + searchText.Length);
    }
  }
  pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
}


I copied the following code from the MSDN documentation of the TextRange.Select method, replaced the RichTextBox with a FlowDocumentScrollViewer and it works as expected. How did you define your TextPointers? That seems to be the most probable cause of your problem.

UPDATE: I updated my code to include your selection algorithm and it still works. The only thing I'm aware of doing different is the "break" after selecting. Otherwise it'd select from the beginning of the first occurence of searchText until after the end of the last occurence. Other than that I could imagine that your searchText might not be included in your document at all (perhaps a casing issue?), but that's just guessing. Did you debug your code? Are the TextPointers valid (not null etc.) when you try to select the text?

XAML:

<FlowDocumentScrollViewer GotMouseCapture="richTB_GotMouseCapture" Name="richTB">
    <FlowDocument>
        <Paragraph Name="myParagraph">
            <Run>
                When the user clicks in the RichTextBox, the selected text changes programmatically.
            </Run>
        </Paragraph>
    </FlowDocument>
</FlowDocumentScrollViewer>

Code:

private void richTB_GotMouseCapture(object sender, MouseEventArgs e)
{
    string searchText = "text";
    TextPointer pointer = richTB.Document.ContentStart;
    while (pointer != null)
    {
        if (pointer.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
        {
            string textRun = pointer.GetTextInRun(LogicalDirection.Forward);
            // where textRun is the text in the flowDocument
            // and searchText is the text that is being searched for
            int indexInRun = textRun.IndexOf(searchText);
            if (indexInRun >= 0)
            {
                TextPointer startPos = pointer.GetPositionAtOffset(indexInRun);
                TextPointer endPos = pointer.GetPositionAtOffset(indexInRun + searchText.Length);
                richTB.Selection.Select(startPos, endPos);
                break;
            }
        }
        pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜