双击选择以句点分隔的单词,例如 "i.a."

Double click selecting words separated by periods like "i.a."

我的目标是双击“i.a”的整个部分。 like this however the default for WPF is it selects each part of the string individually

样本:

<TextBox>Reason for contact: i.a.</TextBox>

到目前为止我发现了什么

我尝试使用 SpellCheck.CustomDictionaries,选词不受这些影响。

试试这个。它根据从最近的左侧空格 ' '(或字符串开头)到最近的右侧空格(或字符串结尾)的插入符位置(即 SelectionStart - 1)选择文本部分。

private void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (sender is TextBox tb)
    {
        char[] chars = tb.Text.ToCharArray();

        int i;
        // Find nearest left whitespace or start of string
        for (i = tb.SelectionStart - 1; i >= 0 && chars[i] != ' '; i--);
        int selectionStart = i + 1;

        // Find nearest right whitespace or end of string
        for (i = tb.SelectionStart; i < chars.Length && chars[i] != ' '; i++);
        int selectionLength = i - selectionStart;
        
        tb.Select(selectionStart, selectionLength);
    }
}