Example #1
0
        /// <summary>
        /// Each time a property change, we have to reposition
        /// </summary>
        private static void OnCarouselPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Carousel carouselControl = (Carousel)d;

            if (e.Property == SelectedIndexProperty)
            {
                var newValue = (int)e.NewValue == -1 ? null : carouselControl.Items[(int)e.NewValue];
                var oldValue = e.OldValue == null ? null : carouselControl.Items.ElementAtOrDefault((int)e.OldValue);
                if (oldValue != null)
                {
                    var item = (CarouselItem)carouselControl.ContainerFromIndex((int)e.OldValue);
                    if (item != null)
                    {
                        item.IsSelected = false;
                    }
                }

                bool isNewSelectedItem = carouselControl.SelectedItem != newValue;

                if (newValue != null)
                {
                    var item = (CarouselItem)carouselControl.ContainerFromIndex((int)e.NewValue);
                    if (item != null)
                    {
                        item.IsSelected = true;
                    }

                    carouselControl.FocusContainerFromIndex((int)e.NewValue);
                }

                // double check
                if (isNewSelectedItem)
                {
                    carouselControl.SetValue(SelectedItemProperty, newValue);
                    var newValues = new List <object>()
                    {
                        newValue
                    };
                    var oldValues = oldValue == null ? new List <object>() : new List <object>()
                    {
                        oldValue
                    };

                    var args = new SelectionChangedEventArgs(oldValues, newValues);
                    carouselControl.SelectionChanged?.Invoke(carouselControl, args);
                    return;
                }
            }
            else if (e.Property == SelectedItemProperty)
            {
                var index = e.NewValue == null ? -1 : carouselControl.Items.IndexOf(e.NewValue);

                // double check
                if (carouselControl.SelectedIndex != index)
                {
                    carouselControl.SetValue(SelectedIndexProperty, index);
                    var newValues = new List <object>()
                    {
                        e.NewValue
                    };
                    var oldValues = e.OldValue == null ? new List <object>() : new List <object>()
                    {
                        e.OldValue
                    };

                    var args = new SelectionChangedEventArgs(oldValues, newValues);
                    carouselControl.SelectionChanged?.Invoke(carouselControl, args);
                    return;
                }
            }

            carouselControl.UpdatePositions();
        }