views:

23

answers:

0

Hi, I'm applying a TextDecoration to a TextRange in a RichTextBox. Works fine except that if the user types at the end of the range, then the decoration is continued.

Eg. if the word "am" is decorated in "I am here" and the user changes "am" to "am not", then all of "am not" will be decorated. I just want "am" to be decorated.

Any idea how I can acheive this please?

Here's how I'm decorating;

...
 TextPointer ptr1, ptr2;
            ptr1 = richTextBox1.Document.ContentStart.GetPositionAtOffset(4);
            ptr2 = richTextBox1.Document.ContentStart.GetPositionAtOffset(6);
            new TextRange(ptr1, ptr2).ApplyPropertyValue(Run.TextDecorationsProperty, GetDecoration());

        ...


        TextDecorationCollection mydecorations = null;
        TextDecorationCollection GetDecoration()
        {
            if (mydecorations == null)
            {
                DrawingGroup drawing = new DrawingGroup();
                DrawingContext context = drawing.Open();
                System.Windows.Media.Pen pen = new System.Windows.Media.Pen(System.Windows.Media.Brushes.Blue, 0.33);
                context.DrawLine(pen, new System.Windows.Point(0.0, 0.0), new System.Windows.Point(.5, 1.0));

                context.Close();
                DrawingBrush brush = new DrawingBrush(drawing);
                brush.TileMode = TileMode.Tile;
                brush.Viewport = new Rect(0.0, 0.0, 3.0, 3.0);
                brush.ViewportUnits = BrushMappingMode.Absolute;
                TextDecoration decoration = new TextDecoration(TextDecorationLocation.Underline, new System.Windows.Media.Pen(brush, 3.0), 0.0, TextDecorationUnit.FontRecommended, TextDecorationUnit.Pixel);
                mydecorations = new TextDecorationCollection();
                mydecorations.Add(decoration);
                mydecorations.Freeze();
            }
            return mydecorations;
        }

Thanks, Jim