Desperately Seeking Love of Sophie

Progressbar Code

with one comment

using System;

using System.Windows;

using System.Windows.Shapes;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Input;

using System.Reflection;namespace SPFControls

{

public class ProgressBar : Control
    {

        #region Private Variables

private Rectangle _progressBarTrackRectangle;
        private Rectangle _progressBarFillRectangle;
        private string _progressBarLook;
        private Canvas _progressBarRoot;
        private double _minValue;
        private double _maxValue;
        private double _value;
        #endregion


        #region Public Constructors

        public ProgressBar()
        {
            Assembly ProgressBarAssembly = Assembly.GetExecutingAssembly();
            System.IO.StreamReader xamlstream = new System.IO.StreamReader((ProgressBarAssembly.GetManifestResourceStream("ProgressBar.ProgressBar.xaml")));
            _progressBarLook = xamlstream.ReadToEnd();
            _progressBarRoot = InitializeFromXaml(_progressBarLook) as Canvas;
            _progressBarFillRectangle = _progressBarRoot.FindName("rectProgressBarFill") as Rectangle;
            _progressBarTrackRectangle = _progressBarRoot.FindName("rectProgressBarTrack") as Rectangle;
            this.Loaded += new EventHandler(ProgressBar_Loaded);
        }

void ProgressBar_Loaded(object sender, EventArgs e)
       {
           UpdateLayout();
       }
       #endregion 

#region Public Properties

        public double Minimum
        {
            get { return _minValue; }
            set { _minValue = value; }
        }

public double Maximum
        {
            get { return _maxValue; }
            set { _maxValue = value; }
        }

public double Value
        {
            get { return _value; }
            set
            {
                if (value <= Maximum && value >= Minimum)
                {
                    _value = value;
                    _progressBarFillRectangle.Width = ((_progressBarTrackRectangle.Width / (_maxValue - _minValue)) * (_value - _minValue));
                }
            }
        }

public new double Height
        {
            get { return ((FrameworkElement)this).Height; }
            set
            {
                ((FrameworkElement)this).Height = value;
                UpdateLayout();
            }
        }

public new double Width
        {
            get { return ((FrameworkElement)this).Width; }
            set
            {
                ((FrameworkElement)this).Width = value;
                UpdateLayout();
            }
        }

private void UpdateLayout()
        {
            _progressBarRoot.Height = Height;
            _progressBarRoot.Width = Width;

_progressBarFillRectangle.Height = Height;
            _progressBarFillRectangle.Width = Width;

_progressBarTrackRectangle.Height = Height;
            _progressBarTrackRectangle.Width = Width;
        }

#endregion    }
}

Written by Vivek

May 4, 2007 at 7:58 pm

One Response

Subscribe to comments with RSS.

  1. [...] code for the progressbar looks like this… Posted: Friday, May 04, 2007 9:00 PM by vivekd Filed under: [...]


Leave a Reply