Home
WPF
ProgressBar: Value and Maximum
Updated Sep 28, 2022
Dot Net Perls
ProgressBar, WPF. A ProgressBar graphically displays progress. As it is updated, by setting its Value property, it displays a colored bar.
To begin, please create a WPF project and drag a ProgressBar to your Window. Now, adjust some of the attributes on the ProgressBar in the XAML.
Attribute example. We set its Foreground, and use its Value and Maximum properties. Change the Foreground—we use a fish color ("Salmon") here.
Detail I added the Name attribute to the ProgressBar. I chose the short (but not descriptive) Name of "B".
Name
Next I added a separate Button control by dragging it to the Window. I specified the "Click" attribute.
Button
Finally In Button_Click, I increase the Value property of the ProgressBar by one-fifth of its Maximum.
<Window x:Class="WpfApplication27.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ProgressBar HorizontalAlignment="Left" Height="10" Margin="10,10,0,0" VerticalAlignment="Top" Width="100" Name="B" Foreground="Salmon"/> <Button Content="Add" HorizontalAlignment="Left" Margin="115,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> </Grid> </Window>
using System.Windows; using System.Windows.Controls; namespace WpfApplication27 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { // ... Add 1/5 to the ProgressBar. B.Value += (B.Maximum / 5); // ... See if ProgressBar has reached its max. if (B.Value == B.Maximum) { // ... Change button Content. Button button = sender as Button; button.Content = "DONE"; } } } }
Program result. If you click on the Button 5 times, the ProgressBar is at 100%. When the Value equals the maximum of the ProgressBar, the Button is changed to display "DONE".
ValueChanged. One useful event handler on the ProgressBar is ValueChanged. It functions in a similar way to ValueChanged on the Slider control.
Slider
In many programs, a ProgressBar is meant to update as a program completes a long-running task. Occasionally, a ProgressBar is used to indicate progress in a sign-up form.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 28, 2022 (edit).
Home
Changes
© 2007-2025 Sam Allen