Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #49 – How to Implement a Progress Bar

With the release of Silverlight 2 RC0 there are three new controls we will be discussing that were not available for beta 2. The three new controls with a Tip of the Day for each include:

For this tip we will be exploring the ProgressBar control. The following code below shows how to declare a ProgressBar in your XAML:

<ProgressBar Foreground="White" Background="Gray" Value="25" Maximum="100" Width="200" Height="20" Margin="20">

Let’s take a look at each of these properties in the ProgressBar and what they do:

  1. Foreground – The color of the acutal bar in the ProgressBar.
  2. Background – The background color of the control.
  3. Value – The starting value for the ProgressBar.
  4. Maximum – The end value for the ProgressBar.
  5. Width/Height – The width & height of the ProgressBar.
  6. Margin – The gap between the control and it’s parent container. I find it easier to use Margin over setting Canvas.Left and Canvas.Top.

Add a timer and you can move the progress bar to it’s Maximum value:

namespace Password
{
    public partial class Page : UserControl
    {
        Storyboard _timer = new Storyboard();
        public Page()
        {
            InitializeComponent();
 
            _timer.Duration = TimeSpan.FromMilliseconds(10);
            _timer.Completed += new EventHandler(_timer_Completed);
            _timer.Begin();
        }
 
        void _timer_Completed(object sender, EventArgs e)
        {
            if (MyProgress.Value < MyProgress.Maximum)
            {
                MyProgress.Value++;
                _timer.Begin();
            }
        }
    }
}

If you run this control as is you will see it start at 25 and end when it’s full:

Begin:

image

End:

image

Now if you don’t want to use a timer and want the progress bar to repeat non-stop you can set the the property IsDeterminate to “true”

<ProgressBar IsIndeterminate="True"/>

image 

You can further customize your ProgressBar by setting the Background and Foreground to any of the available brushes:

  • Brush
  • GradientBrush
  • LinearBrush
  • Imagebrush
  • LinearGradientBrush
  • RadialGradientBrush
  • SolidColorBrush

Here is an example using an ImageBrush:

<ProgressBar  Value="25" x:Name="MyProgress" Maximum="100" Width="300" Height="50" Margin="20">
    <ProgressBar.Foreground>
        <ImageBrush ImageSource="Smile.png"></ImageBrush>
    </ProgressBar.Foreground>
</ProgressBar>

Begin:

image

End:

image

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Silverlight Tip of the Day #49 ??? How to Implement a Progress Bar - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #49 ??? How to Implement a Progress Bar - Silverlight Tips of the Day - Blog by Mike Snow

# September 29, 2008 5:43 PM

Silverlight Tip of the Day #48 ??? How to Implement a Combobox - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #48 ??? How to Implement a Combobox - Silverlight Tips of the Day - Blog by Mike Snow

# September 29, 2008 5:44 PM

Silverlight Tip of the Day #47 ??? How to Implement a Password Box - Silverlight Tips of the Day - Blog by Mike Snow said:

Pingback from  Silverlight Tip of the Day #47 ??? How to Implement a Password Box - Silverlight Tips of the Day - Blog by Mike Snow

# September 29, 2008 5:44 PM

Microsoft Weblogs said:

With the release of Silverlight 2 RC0 there are three new controls we will be discussing that were not

# September 29, 2008 6:56 PM

preishuber said:

dont forget the

videobrush

# September 30, 2008 2:40 AM

2008 September 30 - Links for today « My (almost) Daily Links said:

Pingback from  2008 September 30 - Links for today &laquo; My (almost) Daily Links

# September 30, 2008 3:52 AM

Silverlight news for September 30, 2008 said:

Pingback from  Silverlight news for September 30, 2008

# September 30, 2008 9:45 AM

Dew Drop – September 30, 2008 (Evening Edition) | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop &ndash; September 30, 2008 (Evening Edition) | Alvin Ashcraft's Morning Dew

# September 30, 2008 10:28 PM

Community Blogs said:

In this post: Mike Snow, Bill Reiss, Jesse Liberty, Harsh Bardhan, Tim Heuer, Matthias Shapiro, and Jeff

# October 1, 2008 9:53 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #57 Title: How to Dynamically Load a Silverlight Control within another Silverlight

# October 8, 2008 11:05 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:27 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:32 PM

Websites tagged "progressbar" on Postsaver said:

Pingback from  Websites tagged "progressbar" on Postsaver

# December 11, 2008 7:02 PM

engvinay said:

not working on progress while page load.

# December 24, 2008 1:11 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

spam-dev said:

thank you.

# June 10, 2010 6:18 AM

LetsBlogAbout.NET said:

Silverlight Tips of the Day Update 3

# October 15, 2010 11:02 AM

LetsBlogAbout.NET said:

Silverlight Tips of the Day

# October 15, 2010 11:08 AM

Progress bar in Silverlight « Jan Keijzer’s Weblog said:

Pingback from  Progress bar in Silverlight &laquo; Jan Keijzer&#8217;s Weblog

# October 20, 2010 6:33 AM

manojpatil123 said:

How make disable progressbar after 10 miliseconds?

# August 30, 2011 5:02 AM