选择WPF的RichTextBox(的FlowDocument)编程范围内的文本 [英] Select Range of Text in WPF RichTextBox (FlowDocument) Programmatically

查看:1485
本文介绍了选择WPF的RichTextBox(的FlowDocument)编程范围内的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的WPF的RichTextBox,我想以编程方式选择字母/词一个给定的范围,并突出显示。我已经试过这一点,但它不工作,可能是因为我没有考虑到一些隐藏的FlowDocument标记或类似。例如,我要选择字母3-8 2-6,但被选中):

  VAR开始= MyRichTextBox.Document.ContentStart;
变种startPos = start.GetPositionAtOffset(3);
变种endPos = start.GetPositionAtOffset(8);
VAR的TextRange =新的TextRange(startPos,endPos);
textRange.ApplyPropertyValue(TextElement.ForegroundProperty,
    新的SolidColorBrush(Colors.Blue));
textRange.ApplyPropertyValue(TextElement.FontWeightProperty,
    FontWeights.Bold);

我已经意识到RichTextBox的处理比我想象的有点棘手:)

更新:我在MSDN论坛上的几个答案:<一href=\"http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/01c9faac-2c37-40ca-9ca5-91d433033671/\">This线程其中dekurverSEID:


  

你指定的偏移量不
  字符偏移而偏移的象征。
  你需要做的是得到一个
  TextPointer你知道是相邻
  为文本,那么你可以添加字符
  偏移。


和LesterLobo说:


  

您将需要遍历
  段落和内联找
  下一步,然后他们的偏移在循环
  申请的悉数亮相
  特定的文本。请注意,当你编辑
  你的文字会移动,但你
  亮点难道不移动其
  与偏移不相关联的
  文本。然而,你可以创建一个
  运行自定义,并提供一大亮点为
  它...


还是希望能看到一些示例code本,如果有人知道他们的周围FlowDocuments ...

办法

修改的我被克拉茨VB code工作的一个版本,它看起来像这样:

 私有静态TextPointer用GetPoint(TextPointer开始,INT X)
{
    VAR RET =启动;
    变种I = 0;
    而(I&LT; X&放大器;&安培;沤!= NULL)
    {
        如果(ret.GetPointerContext(LogicalDirection.Backward)==
TextPointerContext.Text ||
            ret.GetPointerContext(LogicalDirection.Backward)==
TextPointerContext.None)
            我++;
        如果(ret.GetPositionAtOffset(1,
LogicalDirection.Forward)== NULL)
            返回RET;
        RET = ret.GetPositionAtOffset(1,
LogicalDirection.Forward);
    }
    返回RET;
}

和我使用它是这样的:

 着色(item.Offset,item.Text.Length,Colors.Blue);私人无效着色(INT抵消,诠释长度,色色)
{
    VAR的TextRange = MyRichTextBox.Selection;
    VAR开始= MyRichTextBox.Document.ContentStart;
    VAR startPos =用GetPoint(启动,抵消);
    VAR endPos =用GetPoint(启动,偏移+长度);    textRange.Select(startPos,endPos);
    textRange.ApplyPropertyValue(TextElement.ForegroundProperty,
新的SolidColorBrush(彩色));
    textRange.ApplyPropertyValue(TextElement.FontWeightProperty,
FontWeights.Bold);
}


解决方案

 公共功能GoToPoint(BYVAL开始作为TextPointer,BYVAL x As中整数)作为TextPointer
    暗淡了作为TextPointer =启动
    昏暗我为整数= 0
    做虽然我和LT; X
        如果out.GetPointerContext(LogicalDirection.Backward)= TextPointerContext.Text或_
             out.GetPointerContext(LogicalDirection.Backward)= TextPointerContext.None然后
            I + = 1
        万一
        如果out.GetPositionAtOffset(1,LogicalDirection.Forward)是没有那么
            返回了
        其他
            OUT = out.GetPositionAtOffset(1 LogicalDirection.Forward)
        万一
    循环
    返回了
结束功能

试试这个,这应该返回文本指针为给定char抵消。 (对不起它在VB,但多数民众赞成什么,我的工作中......)

I have this WPF RichTextBox and I want to programmatically select a given range of letters/words and highlight it. I've tried this, but it doesn't work, probably because I'm not taking into account some hidden FlowDocument tags or similar. For example, I want to select letters 3-8 but 2-6 gets selected):

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);

I've realised RichTextBox handling is a bit trickier than I thought :)

Update: I got a few answers on the MSDN forums: This thread where "dekurver" seid:

The offsets you're specifying are not character offsets but symbol offsets. What you need to do is get a TextPointer that you know is adjacent to text, then you can add character offsets.

And "LesterLobo" said:

you will need to loop through the paragraphs and inlines to find the Next and then their offsets in a loop to apply for all appearances of the specific text. note that when you edit your text would move but your highlight wouldnt move as its associated with the offset not the text. You could however create a custom run and provide a highlight for it...

Would still LOVE to see some sample code for this if someone knows their way around FlowDocuments...

EDIT I got a version of Kratz VB code working, it looks like this:

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;
}

And I use it like this:

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);
}

解决方案

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

Try this, this should return a text pointer for the given char offset. (Sorry its in VB, but thats what I am working in...)

这篇关于选择WPF的RichTextBox(的FlowDocument)编程范围内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆