This project is read-only.

Without Masking you can Fill Star Partially According to Value

Nov 22, 2012 at 9:39 AM
Edited Nov 22, 2012 at 12:45 PM

I did following changes in Star control and it works without background color property and it draw the star according to the value of star. I used Gray color to show 0 value. One property cab be add for it. Here is the code:

private static void OnStarForegroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

{
            Star star = (Star)d;
            //control.starForeground.Fill = (SolidColorBrush)e.NewValue;
            star.FillStarColor();
}

private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Star starControl = (Star)d;
         
            starControl.FillStarColor();

            Int32 marginLeftOffset = (Int32)(starControl.Value * (double)STAR_SIZE);
            //starControl.mask.Margin = new Thickness(marginLeftOffset, 0, 0, 0);
            starControl.InvalidateArrange();
            starControl.InvalidateMeasure();
        }

 private void FillStarColor()
        {
            if (Value == 0)
            {
                starForeground.Fill = new SolidColorBrush(Colors.Gray);
            }
            else if (Value > 0 && Value < 1)
            {
                LinearGradientBrush brush = new LinearGradientBrush();
                brush.StartPoint = new Point(0, 0);
                brush.EndPoint = new Point(1, 0);

                GradientStop stop1 = new GradientStop();
                stop1.Offset = Value;
                stop1.Color = StarForegroundColor.Color;

                GradientStop stop2 = new GradientStop();
                stop2.Offset = 1 - Value;
                stop2.Color = Colors.Gray;

                brush.GradientStops.Add(stop1);
                brush.GradientStops.Add(stop2);
                starForeground.Fill = brush;
            }
            else
            {
                starForeground.Fill = StarForegroundColor;
            }
        }