Esempio n. 1
0
        /// <summary>
        ///     Returns a stream of weighted values based on a percentage.
        /// </summary>
        /// <param name="values">A sequence of values.</param>
        /// <param name="percent">The percentage of values.</param>
        /// <returns>A sequence of percentages.</returns>
        public static IEnumerable <double> GetWeightedValues(this IEnumerable <double> values, double percent)
        {
            double total = values.Sum();

            if (total == 0)
            {
                return(values.Select(_ => 0.0));
            }

            return
                (EnumerableFunctions
                 .Zip(
                     values.Scan((acc, current) => acc + current, 0.0),
                     values,
                     (acc, current) => Tuple.Create(acc, current))
                 .Select(tuple => Tuple.Create(tuple.Item1 / total, tuple.Item2 / total))
                 .Select(tuple =>
            {
                double accumulated = tuple.Item1;
                double current = tuple.Item2;

                if (percent > accumulated && accumulated + current > percent)
                {
                    return (percent - accumulated) * total;
                }
                if (percent <= accumulated)
                {
                    return 0.0;
                }
                return 1.0;
            }));
        }
Esempio n. 2
0
        /// <summary>
        ///     Updates the values of the rating items.
        /// </summary>
        private void UpdateValues()
        {
            IList <RatingItem> ratingItems = GetRatingItems().ToList();

            RatingItem oldSelectedItem = GetSelectedRatingItem();

            IEnumerable <Tuple <RatingItem, double> > itemAndWeights =
                EnumerableFunctions
                .Zip(
                    ratingItems,
                    ratingItems
                    .Select(ratingItem => 1.0)
                    .GetWeightedValues(Value.GetValueOrDefault()),
                    (item, percent) => Tuple.Create(item, percent));

            foreach (Tuple <RatingItem, double> itemAndWeight in itemAndWeights)
            {
                itemAndWeight.Item1.Value = itemAndWeight.Item2;
            }

            RatingItem newSelectedItem = GetSelectedRatingItem();

            // Notify when the selection changes
            if (oldSelectedItem != newSelectedItem)
            {
                if (newSelectedItem != null &&
                    AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementSelected))
                {
                    AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(newSelectedItem);
                    if (peer != null)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementSelected);
                    }
                }
                if (oldSelectedItem != null &&
                    AutomationPeer.ListenerExists(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection))
                {
                    AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(oldSelectedItem);
                    if (peer != null)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.SelectionItemPatternOnElementRemovedFromSelection);
                    }
                }
            }

            if (HoveredRatingItem == null)
            {
                DisplayValue = Value.GetValueOrDefault();
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Updates the value and actual value of the rating items.
        /// </summary>
        private void UpdateDisplayValues()
        {
            IList <RatingItem> ratingItems = GetRatingItems().ToList();

            IEnumerable <Tuple <RatingItem, double> > itemAndWeights =
                EnumerableFunctions
                .Zip(
                    ratingItems,
                    ratingItems
                    .Select(ratingItem => 1.0)
                    .GetWeightedValues(DisplayValue),
                    (item, percent) => Tuple.Create(item, percent));

            RatingItem selectedItem = null;
            Tuple <RatingItem, double> selectedItemAndWeight = itemAndWeights.LastOrDefault(i => i.Item2 > 0.0);

            if (selectedItemAndWeight != null)
            {
                selectedItem = selectedItemAndWeight.Item1;
            }
            else
            {
                selectedItem = GetSelectedRatingItem();
            }

            foreach (Tuple <RatingItem, double> itemAndWeight in itemAndWeights)
            {
                if (SelectionMode == RatingSelectionMode.Individual && itemAndWeight.Item1 != selectedItem)
                {
                    itemAndWeight.Item1.DisplayValue = 0.0;
                }
                else
                {
                    itemAndWeight.Item1.DisplayValue = itemAndWeight.Item2;
                }
            }
        }