Advertisement
Guest User

RateBase

a guest
May 29th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows;
  8.  
  9. namespace RateBar
  10. {
  11.     /// <summary>
  12.     /// Represents a Progress Bar that also reports back a Rate
  13.     /// </summary>
  14.     public class RateBase : RangeBase
  15.     {
  16.         /// <summary>
  17.         /// Initializes a new instance of the <see cref="RateBase"/> class.
  18.         /// </summary>
  19.         protected RateBase()
  20.         { }
  21.  
  22.         /// <summary>
  23.         /// Initializes the <see cref="RateBase"/> class.
  24.         /// </summary>
  25.         static RateBase()
  26.         {
  27.             RateChangedEvent = EventManager.RegisterRoutedEvent("RateChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<double>), typeof(RateBase));
  28.             RateProperty = DependencyProperty.Register("Rate", typeof(double), typeof(RateBase), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(RateBase.OnRateChanged)), new ValidateValueCallback(RateBase.IsValidDoubleValue));
  29.             CaptionProperty = DependencyProperty.Register("Caption", typeof(String), typeof(RateBase), new PropertyMetadata(String.Empty, new PropertyChangedCallback(OnCaptionChanged)));
  30.             MinimumRateProperty = DependencyProperty.Register("RateMinimum", typeof(double), typeof(RateBase), new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(RateBase.OnMinimumRateChanged)), new ValidateValueCallback(RateBase.IsValidDoubleValue));
  31.             MaximumRateProperty = DependencyProperty.Register("RateMaximum", typeof(double), typeof(RateBase), new FrameworkPropertyMetadata(1.0, new PropertyChangedCallback(RateBase.OnMaximumRateChanged), new CoerceValueCallback(RateBase.CoerceMaximumRate)), new ValidateValueCallback(RateBase.IsValidDoubleValue));
  32.         }
  33.  
  34.         #region DependencyProperties
  35.  
  36.         /// <summary>
  37.         /// Identifies the RateChanged routed event.
  38.         /// </summary>
  39.         public static readonly RoutedEvent RateChangedEvent;
  40.  
  41.         /// <summary>
  42.         /// Identifies the CaptionChanged routed event.
  43.         /// </summary>
  44.         public static readonly RoutedEvent CaptionChangedEvent;
  45.  
  46.         /// <summary>
  47.         /// Identifies the Rate dependency property.
  48.         /// </summary>
  49.         public static readonly DependencyProperty RateProperty;
  50.  
  51.         /// <summary>
  52.         /// Identifies the RateMinimumProperty dependency property.
  53.         /// </summary>
  54.         public static readonly DependencyProperty MinimumRateProperty;
  55.  
  56.         /// <summary>
  57.         /// Identifies the RateMaximumProperty dependency property.
  58.         /// </summary>
  59.         public static readonly DependencyProperty MaximumRateProperty;
  60.  
  61.         /// <summary>
  62.         /// Identifies the Caption dependency property.
  63.         /// </summary>
  64.         public static readonly DependencyProperty CaptionProperty;
  65.  
  66.         #endregion
  67.  
  68.         #region Events
  69.  
  70.         /// <summary>
  71.         /// Occurs when the rate changes.
  72.         /// </summary>
  73.         public event RoutedPropertyChangedEventHandler<Double> RateChanged;
  74.  
  75.         /// <summary>
  76.         /// Occurs when the rate caption changes.
  77.         /// </summary>
  78.         public event RoutedPropertyChangedEventHandler<String> CaptionChanged;
  79.  
  80.         #endregion
  81.  
  82.         #region Properties
  83.  
  84.         /// <summary>
  85.         /// Gets or sets the current rate of the rate control.
  86.         /// </summary>
  87.         [Category("Behaviour"), Bindable(true)]
  88.         public Double Rate
  89.         {
  90.             get
  91.             {
  92.                 return (Double)base.GetValue(RateProperty);
  93.             }
  94.             set
  95.             {
  96.                 Rescale(value);
  97.                 base.SetValue(RateProperty, value);
  98.             }
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Provide a change for the RateMaximum to be modified for
  103.         /// re-scaling any graphs etc.
  104.         /// </summary>
  105.         /// <param name="rate">The current rate</param>
  106.         protected virtual void Rescale(Double rate)
  107.         {
  108.             // Attempt to keep a threshold of 80%, if the
  109.             // rate increases above this threshold then
  110.             // increase it. This prevents a graph that just
  111.             // gets completely full.
  112.             if (rate > (this.RateMaximum * 0.8))
  113.                 this.RateMaximum = rate * 1.2;
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Gets or sets the current minimum rate of the rate control.
  118.         /// </summary>
  119.         [Category("Behaviour"), Bindable(true)]
  120.         public Double RateMinimum
  121.         {
  122.             get
  123.             {
  124.                 return (Double)base.GetValue(MinimumRateProperty);
  125.             }
  126.             set
  127.             {
  128.                 base.SetValue(MinimumRateProperty, value);
  129.             }
  130.         }
  131.  
  132.         /// <summary>
  133.         /// Gets or sets the current maximum rate of the rate control.
  134.         /// </summary>
  135.         [Category("Behaviour"), Bindable(true)]
  136.         public Double RateMaximum
  137.         {
  138.             get
  139.             {
  140.                 return (Double)base.GetValue(MaximumRateProperty);
  141.             }
  142.             set
  143.             {
  144.                 base.SetValue(MaximumRateProperty, value);
  145.             }
  146.         }
  147.  
  148.  
  149.         /// <summary>
  150.         /// Gets or sets the caption for the rate control
  151.         /// </summary>
  152.         [Category("Behaviour"), Bindable(true)]
  153.         public String Caption
  154.         {
  155.             get
  156.             {
  157.                 return (String)base.GetValue(CaptionProperty);
  158.             }
  159.             set
  160.             {
  161.                 base.SetValue(CaptionProperty, value);
  162.             }
  163.         }
  164.  
  165.         #endregion
  166.        
  167.         #region Methods
  168.  
  169.         /// <summary>
  170.         /// Called when caption changes.
  171.         /// </summary>
  172.         /// <param name="oldValue">The old value.</param>
  173.         /// <param name="newValue">The new value.</param>
  174.         protected virtual void OnCaptionChanged(String oldValue, String newValue)
  175.         {
  176.             RoutedPropertyChangedEventArgs<String> e = new RoutedPropertyChangedEventArgs<String>(oldValue, newValue);
  177.             e.RoutedEvent = CaptionChangedEvent;
  178.             if (e.RoutedEvent != null)
  179.             {
  180.                 base.RaiseEvent(e);
  181.             }
  182.         }
  183.  
  184.         /// <summary>
  185.         /// Coerces the maximum rate.
  186.         /// </summary>
  187.         /// <param name="d">The d.</param>
  188.         /// <param name="value">The value.</param>
  189.         /// <returns></returns>
  190.         private static object CoerceMaximumRate(DependencyObject d, object value)
  191.         {
  192.             RateBase element = (RateBase)d;
  193.             double minimum = element.RateMinimum;
  194.             if (((double)value) < minimum)
  195.             {
  196.                 return minimum;
  197.             }
  198.             return value;
  199.         }
  200.  
  201.         /// <summary>
  202.         /// Called when the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Maximum"/> property changes.
  203.         /// </summary>
  204.         /// <param name="oldMaximum">Old value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Maximum"/> property.</param>
  205.         /// <param name="newMaximum">New value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Maximum"/> property.</param>
  206.         protected virtual void OnMaximumRateChanged(double oldMaximum, double newMaximum)
  207.         {
  208.         }
  209.  
  210.         /// <summary>
  211.         /// Called when [minimum rate changed].
  212.         /// </summary>
  213.         /// <param name="oldMinimum">The old minimum.</param>
  214.         /// <param name="newMinimum">The new minimum.</param>
  215.         protected virtual void OnMinimumRateChanged(double oldMinimum, double newMinimum)
  216.         {
  217.         }
  218.  
  219.         /// <summary>
  220.         /// Called when the caption changes.
  221.         /// </summary>
  222.         /// <param name="d">The DependencyObject (RateBase).</param>
  223.         /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  224.         private static void OnCaptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  225.         {
  226.             RateBase element = (RateBase)d;
  227.             element.OnCaptionChanged((String)e.OldValue, (String)e.NewValue);
  228.         }
  229.  
  230.         /// <summary>
  231.         /// Called when maximum rate changes.
  232.         /// </summary>
  233.         /// <param name="d">The DependencyObject (RateBase).</param>
  234.         /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  235.         private static void OnMaximumRateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  236.         {
  237.             RateBase element = (RateBase)d;
  238.             element.CoerceValue(RateProperty);
  239.             element.OnMaximumRateChanged((double)e.OldValue, (double)e.NewValue);
  240.         }
  241.  
  242.         /// <summary>
  243.         /// Called when minimum rate changes.
  244.         /// </summary>
  245.         /// <param name="d">The DependencyObject (RateBase).</param>
  246.         /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  247.         private static void OnMinimumRateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  248.         {
  249.             RateBase element = (RateBase)d;
  250.             element.CoerceValue(MaximumRateProperty);
  251.             element.CoerceValue(RateProperty);
  252.             element.OnMinimumRateChanged((double)e.OldValue, (double)e.NewValue);
  253.         }
  254.  
  255.         /// <summary>
  256.         /// Called when the rate changes.
  257.         /// </summary>
  258.         /// <param name="d">The DependencyObject (RateBase).</param>
  259.         /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
  260.         private static void OnRateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  261.         {
  262.             RateBase element = (RateBase)d;
  263.             element.OnRateChanged((double)e.OldValue, (double)e.NewValue);
  264.         }
  265.  
  266.         /// <summary>
  267.         /// Called when rate changes.
  268.         /// </summary>
  269.         /// <param name="oldValue">The old value.</param>
  270.         /// <param name="newValue">The new value.</param>
  271.         protected virtual void OnRateChanged(double oldValue, double newValue)
  272.         {
  273.             RoutedPropertyChangedEventArgs<double> e = new RoutedPropertyChangedEventArgs<double>(oldValue, newValue);
  274.             e.RoutedEvent = RateChangedEvent;
  275.             base.RaiseEvent(e);
  276.         }
  277.  
  278.         /// <summary>
  279.         /// Determines whether the value is a valid double value.
  280.         /// </summary>
  281.         /// <param name="value">The value.</param>
  282.         private static bool IsValidDoubleValue(object value)
  283.         {
  284.             double num = (double)value;
  285.             return (!Double.IsNaN(num) && !double.IsInfinity(num));
  286.         }
  287.  
  288.         #endregion
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement