private void UpdateCaretPosition()
    {
        var rectangle = MainTextBox.GetRectFromCharacterIndex(MainTextBox.CaretIndex);

        Caret.Height = rectangle.Bottom - rectangle.Top;
        Canvas.SetTop(Caret, rectangle.Top);
        Canvas.SetBottom(Caret, rectangle.Bottom);
        var left = Canvas.GetLeft(Caret);

        if (!double.IsNaN(left))
        {
            cursorAnimation.From     = left;
            cursorAnimation.To       = rectangle.Right;
            cursorAnimation.Duration = new Duration(TimeSpan.FromSeconds(.05));
            Caret.BeginAnimation(Canvas.LeftProperty, cursorAnimation);
        }
        else
        {
            Canvas.SetLeft(Caret, rectangle.Right);
        }
    }