我想知道如何在 WPF 中获取当前光标所在的单词RichTextBox
。我知道RichTextBox
有选择属性。但是,这只给了我在RichTextBox
. 相反,即使没有突出显示整个单词,我也想知道光标所在的单词。
任何提示表示赞赏。
我想知道如何在 WPF 中获取当前光标所在的单词RichTextBox
。我知道RichTextBox
有选择属性。但是,这只给了我在RichTextBox
. 相反,即使没有突出显示整个单词,我也想知道光标所在的单词。
任何提示表示赞赏。
将此函数附加到任意 RichTextBox,现在称为 testRTB,并查看输出窗口以获取结果:
private void testRTB_MouseUp(object sender, MouseButtonEventArgs e)
{
TextPointer start = testRTB.CaretPosition; // this is the variable we will advance to the left until a non-letter character is found
TextPointer end = testRTB.CaretPosition; // this is the variable we will advance to the right until a non-letter character is found
String stringBeforeCaret = start.GetTextInRun(LogicalDirection.Backward); // extract the text in the current run from the caret to the left
String stringAfterCaret = start.GetTextInRun(LogicalDirection.Forward); // extract the text in the current run from the caret to the left
Int32 countToMoveLeft = 0; // we record how many positions we move to the left until a non-letter character is found
Int32 countToMoveRight = 0; // we record how many positions we move to the right until a non-letter character is found
for (Int32 i = stringBeforeCaret.Length - 1; i >= 0; --i)
{
// if the character at the location CaretPosition-LeftOffset is a letter, we move more to the left
if (Char.IsLetter(stringBeforeCaret[i]))
++countToMoveLeft;
else break; // otherwise we have found the beginning of the word
}
for (Int32 i = 0; i < stringAfterCaret.Length; ++i)
{
// if the character at the location CaretPosition+RightOffset is a letter, we move more to the right
if (Char.IsLetter(stringAfterCaret[i]))
++countToMoveRight;
else break; // otherwise we have found the end of the word
}
start = start.GetPositionAtOffset(-countToMoveLeft); // modify the start pointer by the offset we have calculated
end = end.GetPositionAtOffset(countToMoveRight); // modify the end pointer by the offset we have calculated
// extract the text between those two pointers
TextRange r = new TextRange(start, end);
String text = r.Text;
// check the result
System.Diagnostics.Debug.WriteLine("[" + text + "]");
}
将 Char.IsLetter(...) 更改为 Char.IsLetterOrDigit(...) 或其他任何适当的值,具体取决于您是否也希望保留数字。
提示:将其提取到单独程序集中的扩展方法中,以便在需要时访问它。
好的,所以为了解决这个问题,我蛮力强迫它。
我使用
curCaret.GetTextInRun(LogicalDirection.Backward)
和
curCaret.GetTextInRun(LogicalDirection.Forward)
以及其他分隔词并获得子字符串的分隔符preCaretString.LastIndexOf(" ")
。
最终我添加了字符串的前半部分和字符串的后半部分以获得当前光标所在的单词。
我敢打赌有更聪明的方法可以做到这一点,但至少这解决了问题postCaretString.IndexOf(" ")
您可以通过 获取光标的当前位置CaretPosition
。
不幸的是,没有简单的方法可以让字符位于插入符号位置的左/右。我知道从 RichTextBox 中获取文本的唯一方法是在这个答案中,这有点令人费解。但它会完成必要的事情。
这是我使用 LINQ 和依赖属性的替代解决方案:
public class SelectionRichTextBox : RichTextBox
{
public SelectionRichTextBox()
{
// Use base class style
SetResourceReference(StyleProperty, typeof(RichTextBox));
}
public static readonly DependencyProperty SelectedWordProperty =
DependencyProperty.Register(
"SelectedWord",
typeof(string),
typeof(SelectionRichTextBox),
new PropertyMetadata("")
);
public string SelectedWord
{
get
{
return (string)GetValue(SelectedWordProperty);
}
set
{
SetValue(SelectedWordProperty, value);
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
TextPointer cursorPosition = CaretPosition;
string strBeforeCursor = cursorPosition.GetTextInRun(LogicalDirection.Backward);
string strAfterCursor = cursorPosition.GetTextInRun(LogicalDirection.Forward);
string wordBeforeCursor = strBeforeCursor.Split().Last();
string wordAfterCursor = strAfterCursor.Split().First();
string text = wordBeforeCursor + wordAfterCursor;
SelectedWord = string.Join("", text
.Where(c => char.IsLetter(c))
.ToArray());
base.OnMouseUp(e);
}
}
之后,您可以像这样在绑定中使用它:
<custom:SelectionRichTextBox
SelectedWord="{Binding SelectedWord, Mode=OneWayToSource}"/>
由于单词被空格分隔,因此您可以遍历插入符号周围的运行,直到找到空格。即使您RichTextBox
甚至包含不同的字体和字体大小,此功能也应该起作用。
public string GetWordByCaret(LogicalDirection direction)
{
// Get the CaretPosition
TextPointer position = this.CaretPosition;
TextPointerContext context = position.GetPointerContext(direction);
string text = string.Empty;
// Iterate through the RichTextBox based on the Start, Text and End of nearby inlines
while (context != TextPointerContext.None)
{
// We are only interested in the text here
//, so ignore everything that is not text
if (context == TextPointerContext.Text)
{
string current = position.GetTextInRun(direction);
// The strings appended based on whether they are before the caret or after it...
// And well...I love switches :)
switch (direction)
{
case LogicalDirection.Backward:
{
int spaceIndex = current.LastIndexOf(' ');
// If space is found, we've reached the end
if (spaceIndex >= 0)
{
int length = current.Length - 1;
if (spaceIndex + 1 <= length)
{
text = current.Substring(spaceIndex + 1, length - spaceIndex) + text;
}
return text;
}
else
text = current + text;
}
break;
default:
{
int spaceIndex = current.IndexOf(' ');
// If space is found, we've reached the end
if (spaceIndex >= 0)
{
int length = current.Length;
if (spaceIndex <= length)
{
text += current.Substring(0, spaceIndex);
}
return text;
}
else
text += current;
}
break;
}
}
// Move to the next position
position = position.GetNextContextPosition(direction);
// Get the next context
if (position != null)
context = position.GetPointerContext(direction);
else
context = TextPointerContext.None;
}
return text;
}
现在你可以像这样得到你插入符号所在的词。
string before = GetWordByCaret(LogicalDirection.Backward);
string after = GetWordByCaret(LogicalDirection.Forward);
string word = before + after; // :)