/// <summary> /// Handles the event which occurs when the selected item has changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void CollectionViewCurrentChanged(object sender, EventArgs e) { CollectionView collectionView = (CollectionView)sender; if (collectionView != null && collectionView.CurrentPosition >= 0 && collectionView.CurrentPosition <= piePieces.Count) { PiePiece piece = piePieces[collectionView.CurrentPosition]; DoubleAnimation a = new DoubleAnimation(); a.To = 10; a.Duration = new Duration(TimeSpan.FromMilliseconds(200)); } }
/// <summary> /// Constructs pie pieces and adds them to the visual tree for this control's canvas /// </summary> private void ConstructPiePieces() { CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (myCollectionView == null) { return; } double halfWidth = this.Width / 2; double innerRadius = halfWidth * HoleSize; // compute the total for the property which is being plotted double total = 0; foreach (Object item in myCollectionView) { total += GetPlottedPropertyValue(item); } // add the pie pieces canvas.Children.Clear(); piePieces.Clear(); double accumulativeAngle = 0; foreach (Object item in myCollectionView) { double wedgeAngle = GetPlottedPropertyValue(item) * 360 / total; PiePiece piece = new PiePiece() { Radius = halfWidth, InnerRadius = innerRadius, CentreX = halfWidth, CentreY = halfWidth, WedgeAngle = wedgeAngle, PieceValue = GetPlottedPropertyValue(item), RotationAngle = accumulativeAngle, Fill = ColorSelector != null?ColorSelector.SelectBrush(item, myCollectionView.IndexOf(item)) : Brushes.Black, // record the index of the item which this pie slice represents Tag = myCollectionView.IndexOf(item), ToolTip = new ToolTip() }; piePieces.Add(piece); canvas.Children.Insert(0, piece); accumulativeAngle += wedgeAngle; } }
/// <summary> /// Handles the event which occurs when the selected item is about to change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void CollectionViewCurrentChanging(object sender, CurrentChangingEventArgs e) { CollectionView collectionView = (CollectionView)sender; if (collectionView != null && collectionView.CurrentPosition >= 0 && collectionView.CurrentPosition <= piePieces.Count) { PiePiece piece = piePieces[collectionView.CurrentPosition]; DoubleAnimation a = new DoubleAnimation(); a.To = 0; a.Duration = new Duration(TimeSpan.FromMilliseconds(200)); piece.BeginAnimation(PiePiece.PushOutProperty, a); } }
/// <summary> /// Handles the event which occurs just before a pie piece tooltip opens /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void PiePieceToolTipOpening(object sender, ToolTipEventArgs e) { PiePiece piece = (PiePiece)sender; CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (collectionView == null) { return; } // select the item which this pie piece represents int index = (int)piece.Tag; if (piece.ToolTip != null) { ToolTip tip = (ToolTip)piece.ToolTip; tip.DataContext = collectionView.GetItemAt(index); } }
/// <summary> /// Handles the MouseUp event from the individual Pie Pieces /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void PiePieceMouseUp(object sender, MouseButtonEventArgs e) { CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (collectionView == null) { return; } PiePiece piece = sender as PiePiece; if (piece == null) { return; } // select the item which this pie piece represents int index = (int)piece.Tag; collectionView.MoveCurrentToPosition(index); }