WPF FlowDocumentScrollViewer Selection.Select不工作

WPF FlowDocumentScrollViewer Selection.Select不工作

问题描述:

我想在我的FlowDocumentScrollViewer中选择文本。WPF FlowDocumentScrollViewer Selection.Select不工作

我能找到TextPointer的开始位置和结束位置。所以我有2个TextPointers ...

TextPointer startPos; 
TextPointer endPos; 

使用这2个TextPointers我试图在我的FlowDocumentScrollViewer中选择文本。我正在这样做...

flowDocumentScrollViewer.Selection.Select(startPos, endPos); 

我希望这会突出显示选定的文本。但它并没有这样做。

为什么这不起作用???

[更新] 这是我如何得到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); 
} 

我从TextRange.Select方法的MSDN文档复制下面的代码,用FlowDocumentScrollViewer取代RichTextBox的,它按预期工作。你是如何定义你的TextPointers的? 似乎是您的问题最可能的原因。

更新:我更新了我的代码以包含您的选择算法,它仍然有效。我知道唯一不同的是选择后的“休息”。否则,它会从searchText的第一次出现开始直到最后一次出现的结束。除此之外,我可以想象你的searchText可能不会被包含在你的文档中(可能是一个套管问题?),但这只是猜测。你调试了你的代码吗?当您尝试选择文本时,TextPointers是否有效(不为空等)?

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> 

代码:

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

感谢您的帮助。看到我上面的编辑。你看到这个代码有问题吗? – dcinadr 2010-07-19 17:57:34

+0

更新了我的答案,以包含您的选择算法。 – andyp 2010-07-19 19:32:38

+0

其实我在你做的同一个地方有“休息”。我正在搜索的文本确实包含在文档中。当我调试它找到搜索文本并创建非空的TextPointers。我真正看到的唯一区别是您正在执行GotMouseCapture事件中的代码。我的活动正在被其他用户控件触发。但我不明白为什么这会是一个问题,只要我能够正确引用FlowDocumentScrollViewer? – dcinadr 2010-07-19 20:14:12