/// <inheritdoc />
        protected override Rectangle UpdateVisual(IRenderContext context, Rectangle oldVisual, INode node)
        {
            var scale     = 1 / context.Zoom;
            var layout    = GetSelectionBounds(node, scale);
            var oldLayout = oldVisual.GetRenderDataCache <RectD>();

            if (layout.GetSize() != oldLayout.GetSize())
            {
                // If the size changed we need to update the rectangle size.
                oldVisual.Width  = layout.Width;
                oldVisual.Height = layout.Height;
                CanvasControl.SetCanvasControlArrangeRect(oldVisual, layout);
                oldVisual.SetRenderDataCache(layout);
                // ReSharper disable CompareOfFloatsByEqualityOperator
            }
            else if (layout.X != oldLayout.X || layout.Y != oldLayout.Y)
            {
                // ReSharper restore CompareOfFloatsByEqualityOperator
                // But if only the position changed, we can get away with just arranging the rectangle in the canvas again.
                CanvasControl.SetCanvasControlArrangeRect(oldVisual, layout);
                oldVisual.SetRenderDataCache(layout);
            }

            // Re-create the cached scaled stroke brush if necessary and set it on the rectangle.
            UpdateStroke(oldVisual, scale);

            return(oldVisual);
        }
        /// <summary>
        ///   Creates the rectangle with the necessary properties.
        /// </summary>
        /// <param name="layout">The intended layout for the rectangle.</param>
        /// <returns></returns>
        private Rectangle CreateRectangle(RectD layout)
        {
            var rect = new Rectangle
            {
                Width  = layout.Width,
                Height = layout.Height,
                Fill   = Fill,
                Stroke = Stroke.Brush,
                SnapsToDevicePixels = true
            };

            // Disable everything remotely related to anti-aliasing and pretty scaling.
            RenderOptions.SetEdgeMode(rect, EdgeMode.Aliased);
            // The bitmap scaling mode is necessary for the scaled stroke brush not to show moiré.
            RenderOptions.SetBitmapScalingMode(rect, BitmapScalingMode.NearestNeighbor);

            CanvasControl.SetCanvasControlArrangeRect(rect, layout);

            return(rect);
        }