WPF - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WPF Progressbar

WPF Progressbar

shape Description

WPF Progressbar is used to display the progress. It works by setting a minimum and maximum value properties and then increments a value, which will give an information about the progress completed.

shape Example

Create a new window, then go to XAML source and write the following code: [csharp]<Window x:Class="WPF_Prograssbar.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF | Prograssbar" Height="350" Width="525" Background="Beige"> <Grid> <ProgressBar HorizontalAlignment="Center" Height="33" Margin="136,110,143,0" VerticalAlignment="Top" Width="238" Name="pb1" Foreground="Green"/> <Button Content="Add" HorizontalAlignment="Left" Margin="206,208,0,0" VerticalAlignment="Top" Width="98" Click="Button_Click" Height="39" Background="LightCoral"/> </Grid> </Window>[/csharp] Now write the following run time code for prograssbar: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WPF_Prograssbar { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { pb1.Value += (pb1.Maximum / 10); if (pb1.Value == pb1.Maximum) { // ... Change button Content. Button button = sender as Button; button.Content = "Success"; } } } } [/csharp] Now run the application.The output appears as: After clicking on Add button, then the progressbar starts.

shape Example

In real time, the above example is not much useful.So to explain progressbar following example is taken. Create a new window, then go to XAML Source and write the following code: [csharp]<Window x:Class="WPF_Prograssbar.Prograssbar2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Prograssbar2" Height="300" Width="300" ContentRendered="Window_ContentRendered" Background="Beige"> <Grid> <ProgressBar Minimum="0" Maximum="75" Name="Prograbarstatus" Height="28" Margin="76,120,81,121" /> </Grid> </Window>[/csharp] Now write the following run-time code for progressbar: [csharp]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WPF_Prograssbar { public partial class Prograssbar2 : Window { public Prograssbar2() { InitializeComponent(); } private void Window_ContentRendered(object sender, EventArgs e) { for (int i = 0; i < 100; i++) { Prograbarstatus.Value++; Thread.Sleep(100); } } } } [/csharp] Now run the application.The output appears as: Notice whether the progressbar is empty or still under working process. After completion of working process, the progressbar fills like below:

shape Example

Using Background Worker Class with Progressbar: Create a new window, then go to XAML source and write the following code: [csharp]<Window x:Class="WPF_Prograssbar.Progressbar3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF | Progressbar" Height="300" Width="400" ContentRendered="Window_ContentRendered" > <Grid> <ProgressBar Minimum="0" Maximum="100" Name="Prograssbar3" Height="25" Width="250" /> <TextBlock Text="{Binding ElementName=Prograssbar3, Path=Value, StringFormat={}{0:0}%}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </Window>[/csharp] Now write the following run-time code for progressbar: [csharp]using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WPF_Prograssbar { /// <summary> /// Interaction logic for Progressbar3.xaml /// </summary> public partial class Progressbar3 : Window { public Progressbar3() { InitializeComponent(); } private void Window_ContentRendered(object sender, EventArgs e) { BackgroundWorker work = new BackgroundWorker(); work.WorkerReportsProgress = true; work.DoWork += work_DoWork; work.ProgressChanged += work_ProgressChange; work.RunWorkerAsync(); } private void work_ProgressChange(object sender, ProgressChangedEventArgs e) { Prograssbar3.Value = e.ProgressPercentage; } private void work_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 100; i++) { (sender as BackgroundWorker).ReportProgress(i); Thread.Sleep(100); } } } } [/csharp] Now run the application.