Ejemplo n.º 1
0
 private static void UpdateItemCollectionHandlers(RatingControl rating, NotifyCollectionChangedEventArgs e)
 {
     if (e.NewItems != null) ManageItemCollectionHandlers(rating, e.NewItems, false);
     if (e.OldItems != null) ManageItemCollectionHandlers(rating, e.OldItems, true);
 }
Ejemplo n.º 2
0
        private static void UpdateItems(RatingControl rating, RatingItemUpdateAction action)
        {
            if (rating == null) return;

            if (rating.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                // Initialization

                RatingItem hotSpottedItem = rating.HotSpottedItem;

                double gaugeValue = rating.PersonalRating;
                PreviewState itemPreviewState = PreviewState.PersonalRated;

                switch (action)
                {
                    case RatingItemUpdateAction.Preview:
                        if (hotSpottedItem == null) action = RatingItemUpdateAction.Reset;
                        break;
                    case RatingItemUpdateAction.Reset:
                        if (rating.PersonalRating == rating.RatingRangeMin)
                        {
                            gaugeValue = rating.CommonRating;
                            itemPreviewState = PreviewState.CommonRated;
                        }
                        break;
                }

                // Items state recompution
                double itemValue = rating.RatingRangeDelta / rating.Items.Count;

                int index = 0, order = 1, indexMax = rating.Items.Count, increment = 1;
                if (rating.SelectionDirection == CustomControls.SelectionDirection.LastToFirst)
                {
                    var temp = index; index = indexMax - 1; indexMax = temp - 1;
                    increment = -1;
                }
                for (RatingItemUpdateAction selector = action; indexMax.CompareTo(index) == increment; index += increment)
                {
                    var item = rating.ItemContainerGenerator.ContainerFromIndex(index) as RatingItem;
                    if (item != null)
                    {
                        switch (selector)
                        {
                            case RatingItemUpdateAction.Clear:
                                item.PreviewState = PreviewState.Unrated;
                                continue;

                            case RatingItemUpdateAction.Preview:
                                item.PreviewState = PreviewState.Highlighted;
                                if (item == hotSpottedItem) selector = RatingItemUpdateAction.Clear;
                                break;

                            case RatingItemUpdateAction.Confirm:
                                item.PreviewState = PreviewState.PersonalRated;
                                if (item == hotSpottedItem) selector = RatingItemUpdateAction.Clear;
                                break;

                            case RatingItemUpdateAction.Reset:
                                double itemFraction = (double)RatingItem.FractionProperty.DefaultMetadata.DefaultValue;
                                if (order * itemValue > gaugeValue)
                                {
                                    var delta = gaugeValue - ((order - 1) * itemValue);
                                    itemFraction = delta > .0 ? delta / itemValue : .0;
                                    if (itemFraction == .0)
                                    {
                                        selector = RatingItemUpdateAction.Clear;
                                        index -= increment;
                                        continue;
                                    }
                                }
                                item.Fraction = itemFraction;
                                item.PreviewState = itemPreviewState;
                                break;
                        }
                        order++;
                    }
                }

                double tempRating = rating.RatingRangeMin + Math.Round((order - 1) * itemValue, 2);

                RatingItem ratingHotSpottedItem = null;
                double ratingHighlightedRating = (double)HighlightedRatingProperty.DefaultMetadata.DefaultValue;
                double ratingPersonalRating = rating.PersonalRating;
                switch (action)
                {
                    case RatingItemUpdateAction.Clear:
                        ratingPersonalRating = (double)rating.RatingRangeMin;
                        break;

                    case RatingItemUpdateAction.Preview:
                        ratingHighlightedRating = tempRating;
                        ratingHotSpottedItem = hotSpottedItem;
                        break;

                    case RatingItemUpdateAction.Confirm:
                        ratingPersonalRating = tempRating;
                        break;
                }
                rating.PersonalRating = ratingPersonalRating;
                rating.HighlightedRating = ratingHighlightedRating;
                rating.HotSpottedItem = ratingHotSpottedItem;
            }
        }
Ejemplo n.º 3
0
 private static void ManageItemCollectionHandlers(RatingControl rating, IList items, bool remove)
 {
     if (rating == null && items == null) return;
     foreach (object obj in items)
     {
         RatingItem item = obj as RatingItem;
         if (item != null)
         {
             if (!remove)
             {
                 item.RatingItemHotSpotChangedEvent += rating.OnRatingItemHotSpotChanged;
             }
             else
             {
                 item.RatingItemHotSpotChangedEvent -= rating.OnRatingItemHotSpotChanged;
             }
         }
     }
 }