Why Adorner Layer is not rendering properly on WPF TextBox?

Viewed 88

I want to draw a wave-line pattern under particular text of a TextBox. But the pattern is not drawn properly in adorner layer.

Sample Link : https://drive.google.com/file/d/1g5148ydo961Hi0A3cA8G3eRnxn8v0arR/view?usp=sharing

In the below image you can see that, wave-line pattern is not rendered properly in many of the places. Highlighted in green are some of the properly rendered patterns. enter image description here

If i drawn a simple line that will also not rendered properly. Notice the image below. Highlighted in green are some of the properly rendered lines. enter image description here

protected override void OnRender(DrawingContext drawingContext)
    {
        TextBox textBox = this.AdornedElement as TextBox;
        //WordList stores the start index of a word and word as a dictionary collection.
        wordList.Clear();
        // SetRedUnderline method will provide  the wordList which needs to be underline.
        SetRedUnderline(textBox.Text);

        //pathPen is used to draw line with square cap at start and end.
        Pen pathPen = new Pen(new SolidColorBrush(Colors.Red), 0.2);
        pathPen.EndLineCap = PenLineCap.Square;
        pathPen.StartLineCap = PenLineCap.Square;
        //pathGeometry is used to draw segment of wave line by usig BezierSegment.
        Point pathStart = new Point(0, 1);
        BezierSegment pathSegment = new BezierSegment(new Point(1, 0), new Point(2, 2), new Point(3, 1), true);
        PathFigure pathFigure = new PathFigure(pathStart, new PathSegment[] { pathSegment }, false);
        PathGeometry pathGeometry = new PathGeometry(new PathFigure[] { pathFigure });
        //squigglyBrush is customized to darw a tile of pathGeometry. 
        DrawingBrush squigglyBrush = new DrawingBrush();
        squigglyBrush.Viewport = new Rect(0, 0, 6, 4.8);
        squigglyBrush.ViewportUnits = BrushMappingMode.Absolute;
        squigglyBrush.TileMode = TileMode.Tile;
        squigglyBrush.Drawing = new GeometryDrawing(null, pathPen, pathGeometry);
        foreach (KeyValuePair<int, object> keyValuePair in wordList)
        {
            Rect startPosition = textBox.GetRectFromCharacterIndex(keyValuePair.Key);
            Rect endPosition = textBox.GetRectFromCharacterIndex(keyValuePair.Key + keyValuePair.Value.ToString().Length - 1, true);
            Rect rectUnion = Rect.Union(startPosition, endPosition);
            drawingContext.DrawLine(new Pen(squigglyBrush,4), rectUnion.BottomLeft, rectUnion.BottomRight);
            //drawingContext.DrawLine(new Pen(Brushes.Red, 1), rectUnion.BottomLeft, rectUnion.BottomRight);
        }
    }

Thanks..!

0 Answers
Related