Click here to Skip to main content
15,893,790 members
Articles / Web Development / HTML

Circular Gauge Custom Control for Silverlight 3 and WPF

Rate me:
Please Sign up or sign in to vote.
4.97/5 (71 votes)
22 Jul 2009BSD4 min read 421.1K   22.3K   157  
An article on creating a circular gauge custom control for Silverlight 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

namespace CircularGaugeDemo
{
    public partial class MainPage : UserControl
    {
        //Private variables
        DispatcherTimer timer;
        Game game1;
        Game game2;
        Game game3;
        Game game4;


        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
           
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //Set the current value of the gauges
             game1 = new Game(0);
             this.myGauge1.DataContext = game1;
             game2 = new Game(0);
             this.myGauge2.DataContext = game2;
             game3 = new Game(0);
             this.myGauge3.DataContext = game3;
             game4 = new Game(0);
             this.myGauge4.DataContext = game4;

            //Start the timer
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(2500);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
            
        }


        void timer_Tick(object sender, EventArgs e)
        {
            //Update random scores
            Random r = new Random();
            game1.Score = r.Next(0, 1000);
            double val = r.Next(1, 10);
            game2.Score = val / 10;
            game3.Score = r.Next(-50, 50);
            game4.Score = r.Next(0, 1000);            

        
        }


       
    }

    /// <summary>
    /// Helper class to simulate a game
    /// </summary>
    public class Game : INotifyPropertyChanged
    {
        private double score;

        public double Score
        {
            get { return score; }
            set { score = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Score"));
            }
            }
        }


        public Game(double scr)
        {
            this.Score = scr;
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #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 BSD License


Written By
Web Developer
United States United States
I have worked mainly with ASP.NET but also have exposure to Windows forms, WPF and Silverlight. I also enjoy developing user interfaces using Expression Blend.

Comments and Discussions