Ejemplo n.º 1
0
        /// <summary>
        /// Each time a property change, we have to reposition
        /// </summary>
        private static void OnCarouselPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            Carousel carouselControl = (Carousel)d;

            if (e.Property == SelectedIndexProperty)
            {
                var newValue = carouselControl.Items[(int)e.NewValue];
                var oldValue = e.OldValue == null ? null : carouselControl.Items.ElementAtOrDefault((int)e.OldValue);
                carouselControl.FocusContainerFromIndex((int)e.NewValue);

                // double check
                if (carouselControl.SelectedItem != newValue)
                {
                    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 = 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();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Each time a property change, we have to reposition
        /// </summary>
        private static void OnCarouselPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            Carousel carouselControl = (Carousel)d;

            if (e.Property == SelectedIndexProperty)
            {
                var item = carouselControl.Items[(int)e.NewValue];
                carouselControl.FocusContainerFromIndex((int)e.NewValue);

                // double check
                if (carouselControl.SelectedItem != item)
                {
                    carouselControl.SetValue(SelectedItemProperty, item);

                    return;
                }
            }
            else if (e.Property == SelectedItemProperty)
            {
                var index = carouselControl.Items.IndexOf(e.NewValue);

                // double check
                if (carouselControl.SelectedIndex != index)
                {
                    carouselControl.SetValue(SelectedIndexProperty, index);

                    return;
                }
            }

            carouselControl.UpdatePositions();
        }