Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Custom Gauge Controls for Windows Phone 7: Part I

, 25 Feb 2013
The first article in this series presents the implementation considerations and the way to use some custom gauges in WP7.
GaugeLibV2-noexe.zip
GaugeLib
GaugeLib.csproj.user
Properties
Themes
GaugeLibV2.zip
Bin
Debug
GaugeLib.dll
GaugeLib.csproj.user
WPScadaControls.zip
WPScadaControls
WPClientV2
ApplicationIcon.png
Background.png
Bin
Debug
ApplicationIcon.png
Background.png
WPClientV2.dll
WPClientV2.xap
WPScadaControlsV2.dll
Release
obj
Debug
TempPE
Properties
SplashScreenImage.jpg
WPClientV2.csproj.user
WPScadaControlsV2
Bin
Debug
WPScadaControlsV2.dll
Release
obj
Debug
TempPE
Themes
Properties
ScaleDiagram.cd
Themes
WPScadaControlsV2.csproj.user
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
{
    [TemplatePart(Name="PART_BAR",Type=typeof(Path))]
    public class RadialBarIndicator:BarIndicator
    {
        Path thePath;
        public RadialBarIndicator()
        {
            DefaultStyleKey = typeof(RadialBarIndicator);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            thePath = GetTemplateChild("PART_BAR") as Path;
        }
        protected override void UpdateIndicatorOverride(Scale owner)
        {
            base.UpdateIndicatorOverride(owner);
            RadialScale scale = Owner as RadialScale;
            if (scale != null)
            {
                SetIndicatorGeometry(scale, Value);
            }
        }
        protected override Size MeasureOverride(Size availableSize)
        {
            //call the base version to set the parent
            base.MeasureOverride(availableSize);
            //return all the available size
            double width = 0, height = 0;
            if (!double.IsInfinity(availableSize.Width))
                width = availableSize.Width;
            if (!double.IsInfinity(availableSize.Height))
                height = availableSize.Height;
            RadialScale scale = Owner as RadialScale;
            if (scale != null)
            {
                //every time a resize happens the indicator needs to be redrawn
                SetIndicatorGeometry(scale, Value);
            }
            return new Size(width, height);
        }

        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            TranslateTransform tt = new TranslateTransform();
            RadialScale scale = Owner as RadialScale;
            if (scale != null)
            {//calculate the geometry again. the first time this was done the owner had a size of (0,0)
                //and so did the indicator. once the owner has the correct size (measureOveride has been called)
                //i should re-calculate the shape of the indicator
                SetIndicatorGeometry(scale, Value);
                Point center = scale.GetIndicatorOffset();
                tt.X = center.X;
                tt.Y = center.Y;
                RenderTransform = tt;
            }
            return base.ArrangeOverride(arrangeBounds);
        }

        protected override void OnValueChanged(double newVal, double oldVal)
        {
            RadialScale scale = Owner as RadialScale;
            if (scale != null)
            {
                SetIndicatorGeometry(scale, Value);
            }
        }
        protected override void OnBarThicknesChanged(int newVal, int oldVal)
        {
            base.OnBarThicknesChanged(newVal, oldVal);
            RadialScale scale = Owner as RadialScale;
            if (scale != null)
            {
                SetIndicatorGeometry(scale, Value);
            }
        }

        //sets the indicator geometry based on the scale and the current value
        private void SetIndicatorGeometry(RadialScale scale, double value)
        {
            if (thePath != null)
            {
                double min = scale.MinAngle;
                double max = scale.GetAngleFromValue(Value);
                if (scale.SweepDirection == SweepDirection.Counterclockwise)
                {
                    min = -min;
                    max = -max;
                }
                double rad = scale.GetIndicatorRadius();
                if (rad > BarThickness)
                {
                    Geometry geom = RadialScaleHelper.CreateArcGeometry(min, max, rad, BarThickness, scale.SweepDirection);
                    //stop the recursive loop. only set a new geometry if it is different from the current one
                    if (thePath.Data == null || thePath.Data.Bounds != geom.Bounds)
                        thePath.Data = geom;
                }
            }
        }
    }
}

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)

Share

About the Author

Florin Badea
Software Developer
Romania Romania
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 25 Feb 2013
Article Copyright 2011 by Florin Badea
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid