我有这个WPF RichTextBox,我想以编程的方式选择一个给定的字母/单词范围并突出显示它。我尝试过这样做,但它不起作用,可能是因为我没有考虑到一些隐藏的FlowDocument标记或类似的东西。例如,我希望选择字母3-8,但选择2-6 ):
var start = MyRichTextBox.Document.ContentStart;
var startPos = start.GetPositionAtOffset(3);
var endPos = start.GetPositionAtOffset(8);
var textRange = new TextRange(startPos,endPos);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty,
new SolidColorBrush(Colors.Blue));
textRange.ApplyPropertyValue(TextElement.FontWeightProperty,
FontWeights.Bold);
我意识到RichTextBox的处理比我想象的要复杂一些:)
更新:我在MSDN论坛上得到了一些答案:这个帖子其中的"dekurver“seid:
您指定的偏移量不是字符偏移,而是符号偏移。您需要做的是获得一个您知道是与文本相邻的TextPointer,然后您可以添加字符偏移。
"LesterLobo“说:
您需要遍历段落和内联以查找下一个段落和它们在循环中的偏移量,以适用于特定文本的所有外观。请注意,当您编辑您的文本将移动,但您的突出显示不会移动,因为它与偏移量,而不是文本。但是,您可以创建一个自定义运行并为其提供一个突出显示.
如果有人知道如何绕过FlowDocuments,我还是很想看到这方面的一些示例代码。
编辑我得到了一个版本的克瑞兹VB代码工作,它看起来如下:
private static TextPointer GetPoint(TextPointer start, int x)
{
var ret = start;
var i = 0;
while (i < x && ret != null)
{
if (ret.GetPointerContext(LogicalDirection.Backward) ==
TextPointerContext.Text ||
ret.GetPointerContext(LogicalDirection.Backward) ==
TextPointerContext.None)
i++;
if (ret.GetPositionAtOffset(1,
LogicalDirection.Forward) == null)
return ret;
ret = ret.GetPositionAtOffset(1,
LogicalDirection.Forward);
}
return ret;
}
我就是这样用的:
Colorize(item.Offset, item.Text.Length, Colors.Blue);
private void Colorize(int offset, int length, Color color)
{
var textRange = MyRichTextBox.Selection;
var start = MyRichTextBox.Document.ContentStart;
var startPos = GetPoint(start, offset);
var endPos = GetPoint(start, offset + length);
textRange.Select(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty,
new SolidColorBrush(color));
textRange.ApplyPropertyValue(TextElement.FontWeightProperty,
FontWeights.Bold);
}
发布于 2009-09-23 19:01:34
Public Function GoToPoint(ByVal start As TextPointer, ByVal x As Integer) As TextPointer
Dim out As TextPointer = start
Dim i As Integer = 0
Do While i < x
If out.GetPointerContext(LogicalDirection.Backward) = TextPointerContext.Text Or _
out.GetPointerContext(LogicalDirection.Backward) = TextPointerContext.None Then
i += 1
End If
If out.GetPositionAtOffset(1, LogicalDirection.Forward) Is Nothing Then
Return out
Else
out = out.GetPositionAtOffset(1, LogicalDirection.Forward)
End If
Loop
Return out
End Function
尝试此操作,这将返回给定字符偏移量的文本指针。(对不起,它是用VB编写的,但这正是我所从事的.)
发布于 2009-09-21 13:15:12
试一试:
var textRange = MyRichTextBox.Selection;
var start = MyRichTextBox.Document.ContentStart;
var startPos = start.GetPositionAtOffset(3);
var endPos = start.GetPositionAtOffset(8);
textRange.Select(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
发布于 2012-09-13 03:44:26
我尝试使用KratzVB发布的解决方案,但发现它忽略了换行符。如果您希望计数\r和\n符号,则此代码应该工作:
private static TextPointer GetPoint(TextPointer start, int x)
{
var ret = start;
var i = 0;
while (ret != null)
{
string stringSoFar = new TextRange(ret, ret.GetPositionAtOffset(i, LogicalDirection.Forward)).Text;
if (stringSoFar.Length == x)
break;
i++;
if (ret.GetPositionAtOffset(i, LogicalDirection.Forward) == null)
return ret.GetPositionAtOffset(i-1, LogicalDirection.Forward)
}
ret=ret.GetPositionAtOffset(i, LogicalDirection.Forward);
return ret;
}
https://stackoverflow.com/questions/1454440
复制相似问题