/// <summary> /// The <see cref="Center"/> PropertyChangedCallback function. /// </summary> /// <param name="d">The <see cref="CarouselItem"/> object whose <see cref="Center"/> property changed.</param> /// <param name="e">DependencyPropertyChangedEventArgs which contains the old and new values.</param> private static void OnCenterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CarouselItem item = d as CarouselItem; if (d != null) { item.CenterChanged((Point)e.NewValue); } }
/// <summary> /// The <see cref="Axis"/> PropertyChangedCallback static function. /// </summary> /// <param name="d">The <see cref="CarouselItem"/> object whose <see cref="Axis"/> property changed.</param> /// <param name="e">DependencyPropertyChangedEventArgs which contains the old and new values.</param> private static void OnAxisPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CarouselItem item = d as CarouselItem; if (d != null) { item.AxisChanged((Size)e.NewValue); } }
/// <summary> /// <see cref="Angle"/> PropertyChangedCallback static function. /// </summary> /// <param name="d">The <see cref="CarouselItem"/> object whose <see cref="Angle"/> property changed.</param> /// <param name="e">DependencyPropertyChangedEventArgs which contains the old and new values.</param> private static void OnAnglePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CarouselItem item = d as CarouselItem; if (d != null) { item.AngleChanged((double)e.OldValue, (double)e.NewValue); } }
// User click the item with left mouse button void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (SelectedItemViewer != null) { CarouselItem item = sender as CarouselItem; ItemSource source = item.Tag as ItemSource; SelectedItemViewer.Source = source.ImageSource; SelectedItemViewer.Title = source.Title; SelectedItemViewer.NavigateUri = source.NavigateUri; OnSelectedItemChanged(source); } }
/// <summary> /// Create the total storyboard contains all item's animations. /// </summary> /// <param name="items">The carousel items collection.</param> /// <param name="direction">The carousel turnning direction.</param> /// <returns>The storyboard just created.</returns> private Storyboard BuildStoryboard(UIElementCollection items, TurnDirection direction) { // The carousel storyboard Storyboard sb = null; if (this.Resources.Contains(CarouselStoryboardKey)) { // if already in resources, get from resources sb = this.Resources[CarouselStoryboardKey] as Storyboard; // Must stop it before we change it sb.Stop(); // and clear the children sb.Children.Clear(); // because we rebuild all item } else { sb = new Storyboard(); // Add to resources dictionary this.Resources.Add(CarouselStoryboardKey, sb); } // Now, we start build each item's storyboard int itemsCount = items.Count; double perAngle = _2PI / itemsCount; double by = _2PI; for (int i = 0; i < itemsCount; i++) { double angle = i * perAngle + Math.PI / 2; // We only care the carouselitem CarouselItem item = items[i] as CarouselItem; if (item != null) { item.Angle = angle; // Add item's storyboard to main storyboard if (direction == TurnDirection.Clockwise) { sb.Children.Add(BuildStoryboard(item, by, Duration)); } else { sb.Children.Add(BuildStoryboard(item, -by, Duration)); } } } return(sb); }
// The carousel item height changed private void ItemHeightChanged(double itemHeight) { if (CarouselCanvas != null) { foreach (var item in CarouselCanvas.Children) { CarouselItem ci = item as CarouselItem; if (ci != null) { ci.Width = itemHeight; } } } }
/// <summary> /// Place a item to carousel canvas. /// </summary> /// <param name="item">The item will be placed.</param> /// <returns>The <see cref="CarouselItem"/> just placed.</returns> private CarouselItem PlaceItem(ItemSource item) { CarouselItem cItem = new CarouselItem(); cItem.Width = ItemWidth; cItem.Height = ItemHeight; cItem.Source = item.ImageSource; cItem.Tag = item; // Attach the handle cItem.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown); // Add item to carousel canvas CarouselCanvas.Children.Add(cItem); return(cItem); }
/// <summary> /// Build storyboard to animate the item. /// </summary> /// <param name="name">The name of the storyboard.</param> /// <param name="from">The from angle.</param> /// <param name="to">The end angle.</param> /// <param name="duration">The duration of the animation.</param> /// <returns>The builded storyboard.</returns> //private Storyboard BuildStoryboard(CarouselItem item, double from, double to, Duration duration) //{ // Storyboard storyboard = new Storyboard(); // //this.Resources.Add(name, storyboard); // // Angle animation // DoubleAnimation doubleAnimation = new DoubleAnimation(); // doubleAnimation.From = from; // doubleAnimation.To = to; // doubleAnimation.Duration = duration; // doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; // // Set storyboard target property // storyboard.Children.Add(doubleAnimation); // Storyboard.SetTarget(doubleAnimation, item); // Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Angle")); // return storyboard; //} /// <summary> /// Build storyboard to animate the item. /// </summary> /// <param name="name">The name of the storyboard.</param> /// <param name="by">The total amount by which the animation changes its starting value.</param> /// <returns>The builded storyboard.</returns> private Storyboard BuildStoryboard(CarouselItem item, double by, Duration duration) { Storyboard storyboard = new Storyboard(); //this.Resources.Add(name, storyboard); // Angle animation DoubleAnimation doubleAnimation = new DoubleAnimation(); doubleAnimation.By = by; doubleAnimation.Duration = duration; doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; // Set storyboard target property storyboard.Children.Add(doubleAnimation); Storyboard.SetTarget(doubleAnimation, item); Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Angle")); return(storyboard); }
/// <summary> /// Turnning one step in specifies direction. /// </summary> /// <param name="direction">The turnning direction.</param> /// <remarks>You can call this function only /// the <see cref="AutoTurn"/> is set to <c>false</c>.</remarks> public void TrunOnStep(TurnDirection direction) { if (AutoTurn) { return; } if (CarouselCanvas != null) { double perAngle = _2PI / CarouselCanvas.Children.Count; foreach (var item in CarouselCanvas.Children) { CarouselItem cItem = item as CarouselItem; if (cItem != null) { if (direction == TurnDirection.Clockwise) { cItem.Angle += perAngle; } else { cItem.Angle -= perAngle; } // if overceed 2 PI, then adjust it if (cItem.Angle > _2PI) { cItem.Angle -= _2PI; } else if (cItem.Angle < -_2PI) { cItem.Angle += _2PI; } } } } }
// The item collection changed private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (CarouselCanvas != null) { switch (e.Action) { case NotifyCollectionChangedAction.Add: // Add item to collection { // X and Y axis of the ellipse double radiusX = (this.CarouselCanvas.ActualWidth - 2 * ItemWidth - (Padding.Left + Padding.Right)) / 2; double radiusY = (this.CarouselCanvas.ActualHeight - 2 * ItemHeight - (Padding.Top + Padding.Bottom)) / 2; // center point of the ellipse double centerX = radiusX + ItemWidth / 2 + Padding.Left; double centerY = radiusY + Padding.Top; Point center = new Point(centerX, centerY); Size axis = new Size(radiusX, radiusY); int count = e.NewItems.Count; for (int i = 0; i < count; i++) { CarouselItem cItem = PlaceItem(e.NewItems[i] as ItemSource); cItem.Center = center; cItem.Axis = axis; } Storyboard sb = BuildStoryboard(CarouselCanvas.Children, this.TurnDirection); if (this.AutoTurn) { sb.Begin(); } } break; case NotifyCollectionChangedAction.Remove: // Remove item from collection { int count = e.OldItems.Count; for (int i = 0; i < count; i++) { CarouselCanvas.Children.RemoveAt(e.OldStartingIndex + i); } Storyboard sb = BuildStoryboard(CarouselCanvas.Children, this.TurnDirection); if (this.AutoTurn) { sb.Begin(); } } break; case NotifyCollectionChangedAction.Replace: // Replace item { int count = e.OldItems.Count; for (int i = 0; i < count; i++) { CarouselItem item = CarouselCanvas.Children[e.OldStartingIndex + i] as CarouselItem; item.Source = (e.NewItems[i] as ItemSource).ImageSource; } } break; case NotifyCollectionChangedAction.Reset: // Reset the collection { // do nothing } break; } } }