如何将弹出窗口放在单词的开头 - wpf

时间:2017-04-29 11:29:19

标签: c# wpf

使用以下代码,我可以在插入位置显示弹出窗口,如何在单词的开头打开弹出窗口。我正在使用richtextbox。

 Rect positionRect =  richtextbox.CaretPosition.GetCharacterRect(LogicalDirection.Backward);
 Point point = richtextbox.PointToScreen(positionRect.BottomRight);
 popup.HorizontalOffset = point.X;
 popup.VerticalOffset = point.Y;
 popup.IsOpen = true;

enter image description here

1 个答案:

答案 0 :(得分:0)

我使用<RichTextBox x:Name="Rtb1"/>测试下面的代码,然后使用RTB中的文本测试#34; stackoverflow总是很好&#34;。将光标放在某个点并单击按钮,在单词的开头出现一个弹出窗口。现在,我使用空格作为单词分隔符,如果你想使用(,)或其他任何东西,你可以相应地修改它。

private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextPointer cur = Rtb1.CaretPosition;
            char[] textbuffer = new char[1];
            do
            {
                int offset = Rtb1.Document.ContentStart.GetOffsetToPosition(cur);
                cur.GetTextInRun(LogicalDirection.Backward, textbuffer, 0, 1);
                System.Diagnostics.Debug.WriteLine(textbuffer[0].ToString());
                cur = cur.GetNextInsertionPosition(LogicalDirection.Backward);
            } while (textbuffer[0] != ' ' && cur != null);

            cur = cur.GetNextInsertionPosition(LogicalDirection.Forward);
            if (cur == null)
                cur = Rtb1.Document.ContentStart;
            Rtb1.CaretPosition = cur;
            Rtb1.Focus();

            Rect rect = cur.GetCharacterRect(LogicalDirection.Backward);
            Point point = Rtb1.PointToScreen(rect.BottomRight);
            Popup1.HorizontalOffset = point.X;
            Popup1.VerticalOffset = point.Y;
            Popup1.IsOpen = true;
        }

请告诉我这是否解决了您的问题。