C# RichTextBox用鼠标点击后获取完整单词

C# RichTextBox用鼠标点击后获取完整单词,c#,wpf,richtextbox,C#,Wpf,Richtextbox,如何在单击时获取整个单词? 我知道类似的问题已经出现了,我也看过了,但出于某种原因,对于40行代码,有一些奇怪且非常古老的解决方案。也许还有更现代的吗?使用eventPreviewMouseDown选择整个单词。当然,根据您想要的功能,您可以使用类似的事件 private void richTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) { TextSelection ts = richTextBox.Sel

如何在单击时获取整个单词?
我知道类似的问题已经出现了,我也看过了,但出于某种原因,对于40行代码,有一些奇怪且非常古老的解决方案。也许还有更现代的吗?

使用event
PreviewMouseDown
选择整个单词。当然,根据您想要的功能,您可以使用类似的事件

private void richTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    TextSelection ts = richTextBox.Selection;
    string text = ts.Text;
    if (!string.IsNullOrEmpty(text))
        MessageBox.Show(text);
}

下面的代码演示了如何在
RichTextBox
控件中获取当前插入符号位置上的单词

XAML:

<Window ... >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <RichTextBox Grid.Row="0" x:Name="rtb" AllowDrop="True" VerticalScrollBarVisibility="Auto" Padding="2"
                    PreviewMouseUp="rtb_PreviewMouseUp" >
            <FlowDocument>
                <Paragraph FontSize="18" TextAlignment="Left" >

                   <!-- The RichTextBox control content should defined be here
                        or use the PASTE command to add some content... -->

                </Paragraph>
            </FlowDocument>
        </RichTextBox> 
        <Button Grid.Row="2" Click="Button_Click">Mark Current Word</Button>                   
    </Grid>
</Window>
使用
GetEdgeTextPointer()
方法确定当前指向的单词的示例:

public void GetCurrentWord()
{          
    TextPointer current = rtb.CaretPosition;
    var start = current.GetEdgeTextPointer(LogicalDirection.Backward); // Word position before caret
    var end = current.GetEdgeTextPointer(LogicalDirection.Forward); // Word position after caret

    if (start is TextPointer && end is TextPointer && start.CompareTo(end) != 0)
    {
        TextRange textrange = new TextRange(start, end);
        // Print the found word to the debug window.
        System.Diagnostics.Debug.WriteLine(textrange.Text);

        // Select the found word
        rtb.Selection.Select(start, end);
    }
    rtb.Focus();
}

private void rtb_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    GetCurrentWord();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    GetCurrentWord();
}

对于
RichTextBox
控件,它有几种内联线类型,并且在扫描时应该对它们进行分析,40行代码不是很好的解决方案。你能至少给我们看看这段代码吗?我们会努力改进它的。@Jackdaw谢谢你关注这个问题。我阅读了MSDN文档,熟悉了主题领域,发现RichTextBox在2021年毫无用处。这是与WebBrowser相同的不必要组件。
public void GetCurrentWord()
{          
    TextPointer current = rtb.CaretPosition;
    var start = current.GetEdgeTextPointer(LogicalDirection.Backward); // Word position before caret
    var end = current.GetEdgeTextPointer(LogicalDirection.Forward); // Word position after caret

    if (start is TextPointer && end is TextPointer && start.CompareTo(end) != 0)
    {
        TextRange textrange = new TextRange(start, end);
        // Print the found word to the debug window.
        System.Diagnostics.Debug.WriteLine(textrange.Text);

        // Select the found word
        rtb.Selection.Select(start, end);
    }
    rtb.Focus();
}

private void rtb_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    GetCurrentWord();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    GetCurrentWord();
}