/// <summary>
        /// Sets the <see cref="ColorStop.IsSelected"/> properties of the <see cref="ColorStop"/>
        /// controls.
        /// </summary>
        /// <remarks>The selection is determined by _selectedIndex.</remarks>
        private void SetSelection()
        {
            if (_colorStopPanel == null || GradientStops == null)
            {
                return;
            }

            foreach (var child in _colorStopPanel.Children)
            {
                ColorStop control = child as ColorStop;
                if (control != null)
                {
                    GradientStop gradientStop = (GradientStop)control.Tag;
                    control.IsSelected = (_selectedIndex == GradientStops.IndexOf(gradientStop));
                }
            }
        }
        /// <summary>
        /// Updates the color stop controls in the ColorStopPanel.
        /// </summary>
        private void UpdateColorStops()
        {
            if (_colorStopPanel == null)
            {
                return;
            }

            // Remember the focused color stop.
            var          oldFocusedStopControl = _colorStopPanel.Children.OfType <ColorStop>().FirstOrDefault(cs => cs.IsFocused);
            GradientStop oldFocusedStop        = null;

            if (oldFocusedStopControl != null)
            {
                oldFocusedStop = oldFocusedStopControl.Tag as GradientStop;
            }

            // Remove previous ColorStop controls.
            RemoveColorStopControls();

            if (GradientStops != null)
            {
                // Create new ColorStop controls.
                foreach (GradientStop stop in GradientStops)
                {
                    var colorStop = new ColorStop
                    {
                        Color     = stop.Color,
                        Focusable = true,
                        Tag       = stop
                    };
                    colorStop.SetBinding(
                        WidthProperty, new Binding("ColorStopSize.Width")
                    {
                        Source = this, Mode = BindingMode.OneWay
                    });
                    colorStop.SetBinding(
                        HeightProperty, new Binding("ColorStopSize.Height")
                    {
                        Source = this, Mode = BindingMode.OneWay
                    });
                    colorStop.SetBinding(
                        SnapsToDevicePixelsProperty, new Binding("SnapsToDevicePixels")
                    {
                        Source = this, Mode = BindingMode.OneWay
                    });
                    colorStop.GotFocus  += OnColorStopGotFocus;
                    colorStop.MouseDown += OnColorStopMouseDown;
                    _colorStopPanel.Children.Add(colorStop);

                    // Restore focus.
                    if (oldFocusedStop != null)
                    {
                        if (stop.Color == oldFocusedStop.Color && stop.Offset == oldFocusedStop.Offset)
                        {
                            colorStop.Focus();
                            oldFocusedStop = null;
                        }
                    }

                    // Position control.
                    double x;
                    switch (colorStop.HorizontalAlignment)
                    {
                    case HorizontalAlignment.Left:
                        x = stop.Offset * ActualWidth;
                        break;

                    case HorizontalAlignment.Right:
                        x = stop.Offset * ActualWidth - colorStop.Width;
                        break;

                    default:
                        x = stop.Offset * ActualWidth - colorStop.Width / 2;
                        break;
                    }

                    Canvas.SetLeft(colorStop, x);
                }

                SetSelection();
            }
        }