private void RenderCaret(DrawingContext context)
{
var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color;
var caretBrush = Brushes.Black;
if (backgroundColor.HasValue)
{
var red = (byte)~backgroundColor.Value.R;
var green = (byte)~backgroundColor.Value.G;
var blue = (byte)~backgroundColor.Value.B;
caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
}
if (_caretBlink && CaretIndex != -1)
{
if (CaretIndex > TextDocument.TextLength)
{
CaretIndex = TextDocument.TextLength;
}
var charPos = VisualLineGeometryBuilder.GetViewPortPosition(this, CaretIndex);
var x = Math.Floor(charPos.X) + 0.5;
var y = Math.Floor(charPos.Y) + 0.5;
var b = Math.Ceiling(charPos.Bottom) - 0.5;
context.DrawLine(
new Pen(caretBrush, 1),
new Point(charPos.TopLeft.X, charPos.TopLeft.Y),
new Point(charPos.TopLeft.X, charPos.BottomLeft.Y));
}
}