csharp/aduskin/AduSkin/src/AduSkin/Controls/Metro/RatingBar/RatingBar.cs

RatingBar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Collections;
using System.Windows.Input;
using System.Windows.Media;
using System.Text.RegularExpressions;

namespace AduSkin.Controls.Metro
{
   /// 
   /// 星级评价控件
   /// 
   public clast RatingBar : Control
   {
      #region Private属性
      /// 
      /// 保存评分控件内部的一个个子Item,比如一个个笑脸或者星星
      /// 
      private ObservableCollection RatingButtonsInternal = new ObservableCollection();
      /// 
      /// 该Command在样式xaml中执行
      /// 
      public static RoutedCommand ValueChangedCommand = new RoutedCommand();
      /// 
      /// 用于保存老的分数
      /// 
      /// 鼠标悬浮在控件上时,悬浮在哪个Item上面则预览该分数,如果鼠标没有按下(即没有确认),则当鼠标移开后应该将分数恢复至老的分数
      /// 
      private double mOldValue;
      /// 
      /// 用于保存是否已经选择了一个分数
      /// 
      /// 鼠标悬浮在控件上时,悬浮在哪个Item上面则预览该分数,如果鼠标没有按下(即没有确认),则当鼠标移开后应该将分数恢复至老的分数
      /// 
      private bool mIsConfirm;
      #endregion

      #region 依赖属性定义

      #region Maximum 最大值
      public int Maximum
      {
         get { return (int)GetValue(MaximumProperty); }
         set { SetValue(MaximumProperty, value); }
      }

      public static readonly DependencyProperty MaximumProperty =
          DependencyProperty.Register("Maximum", typeof(int), typeof(RatingBar), new PropertyMetadata(5, MaximumChangedCallback));


      private static void MaximumChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
         ((RatingBar)d).CreateRatingButtons();
      }
      #endregion

      #region Minimum 最小值
      public int Minimum
      {
         get { return (int)GetValue(MinimumProperty); }
         set { SetValue(MinimumProperty, value); }
      }

      public static readonly DependencyProperty MinimumProperty =
          DependencyProperty.Register("Minimum", typeof(int), typeof(RatingBar), new PropertyMetadata(1, MinimumChangedCallback, CoreMinimumCallback));

      private static object CoreMinimumCallback(DependencyObject d, object baseValue)
      {
         int value = (int)baseValue;
         if (value < 1)
         {
            return 1;
         }
         return value;
      }

      private static void MinimumChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
         ((RatingBar)d).CreateRatingButtons();
      }
      #endregion

      #region Value 当前值
      public double Value
      {
         get { return (double)GetValue(ValueProperty); }
         set { SetValue(ValueProperty, value); }
      }

      public static readonly DependencyProperty ValueProperty =
          DependencyProperty.Register("Value", typeof(double), typeof(RatingBar), new PropertyMetadata(0d, ValueChangedCallback, ValueCoerce));

      private static object ValueCoerce(DependencyObject d, object baseValue)
      {
         RatingBar ratingBar = d as RatingBar;
         double value = 0.0;
         if (double.TryParse(Convert.ToString(baseValue), out value))
         {
            if (value < ratingBar.Minimum)
            {
               value = 0;
            }
            else if (value > ratingBar.Maximum)
            {
               value = ratingBar.Maximum;
            }
         }
         return value;
      }

      private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
         RatingBar ratingBar = d as RatingBar;
         double newValue = Convert.ToDouble(e.NewValue);
         var buttonList = ((RatingBar)d).RatingButtonsInternal;

         foreach (var ratingBarButton in buttonList)
         {
            ratingBarButton.IsHalf = false;
            ratingBarButton.IsSelected = ratingBarButton.Value