Click here to Skip to main content
15,881,882 members
Articles / Mobile Apps / Windows Phone 7

Custom Gauge Controls for Windows Phone 7: Part I

Rate me:
Please Sign up or sign in to vote.
4.91/5 (61 votes)
25 Feb 2013CPOL15 min read 143.3K   3.5K   78  
The first article in this series presents the implementation considerations and the way to use some custom gauges in WP7.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace GaugeLib
{
    public abstract class Indicator:Control
    {
        #region fields
        private Scale owner;
        #endregion

        #region properties
        public Scale Owner
        {
            get
            {
                return this.owner;
            }
            internal set
            {
                if (this.owner != value)
                {
                    this.owner = value;
                    UpdateIndicator(owner);
                }
            }
        }
        #endregion

        #region dependency properties
        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(Indicator), new PropertyMetadata(0.0, ValuePropertyChanged));

        #endregion

        #region dp handlers
        private static void ValuePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            Indicator ind = o as Indicator;
            if (ind != null)
            {
                if (ind.Owner!=null && ind.Value >= ind.Owner.Minimum && ind.Value <= ind.Owner.Maximum)
                    ind.OnValueChanged((double)e.NewValue, (double)e.OldValue);                
            }
        }
        protected virtual void OnValueChanged(double newVal, double oldVal) { }
        #endregion

        #region abstract methods
        protected virtual void UpdateIndicatorOverride(Scale owner) { }
        #endregion

        #region private helper methods
        private void UpdateIndicator(Scale owner)
        {
            if (owner != null)
            {
                if (Value < owner.Minimum)
                    Value = owner.Minimum;
                if (Value > owner.Maximum)
                    Value = owner.Maximum;
            }
            UpdateIndicatorOverride(owner);
        }
        
        #endregion
        //this wasn't virtual in the previous version
        public virtual void Arrange(Size finalSize)
        {
            base.Arrange(new Rect(new Point(), finalSize));
        }
        #region overrides
        protected override Size MeasureOverride(Size availableSize)
        {
            //the main purpose of this override is to set the owner for the 
            //indicator. The actual measuring calculation will be done in 
            //the derived classes
            DependencyObject parent = base.Parent;
            while (parent != null)
            {
                Scale scale = parent as Scale;
                if (scale != null)
                {
                    this.Owner = scale;
                    break;
                }
                FrameworkElement el = parent as FrameworkElement;
                if (el != null)
                {
                    parent = el.Parent;
                }
            }
            return base.MeasureOverride(availableSize);
        }
        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions