Using XAML ProgressBar in WPF

The XAML ProgressBar represents a progress bar. This article shows you how to create and use a ProgressBar control available in Windows Presentation Foundation (WPF) and XAML.

Introduction

The ProgressBar tag in XAML represents a WPF ProgressBar control.

  1. <ProgressBar></ProgressBar>  
The Width and Height properties represent the width and the height of a ProgressBar. The Name property represents the name of the control, that is a unique identifier of a control. The Margin property tells the location of a ProgressBar on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.

The following code snippet sets the name, height and width of a ListView control. The code also sets the horizontal alignment to left and the vertical alignment to top.
  1. <ProgressBar Margin="10,10,0,13" Name="ListView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" Height="30" />  
The progress bar looks as in Figure 1.


Figure 1.


Setting up ProgressBar Value

The Value property of ProgressBar sets up the current value of a ProgressBar control. In the following code, I set the Value property to 60 and now ProgressBar looks as in Figure 2.
  1. <ProgressBar Margin="10,10,0,13" Name="PBar" HorizontalAlignment="Left"   
  2.       VerticalAlignment="Top" Width="300" Height="30" Value="60" >   
  3. </ProgressBar>  

Figure 2.

Dynamically Setting a ProgressBar Value


We can use a Timer or animation to set a ProgressBar value dynamically. The following code creates a DoubleAnimation object and sets ProgressBar.Value by using the ProgressBar.BeginAnimation method.
  1. private void Window_Loaded(object sender, RoutedEventArgs e)  
  2. {  
  3.    Duration duration = new Duration(TimeSpan.FromSeconds(20));  
  4.    DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);  
  5.    PBar.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);  
  6. }  
When you run the application the ProgressBar looks as in Figure 3.


Figure 3.

Flow Direction


The FlowDirection property sets the flow of the ProgressBar. You can set this value to either LeftToRight or RightToLeft. The default value is LeftToRight.
  1. FlowDirection="RightToLeft"  
Adding a ProgressBar to a StatusBar

You probably saw several applications like Internet Explorer where you can see the status of a page load in the status bar at the bottom by using a ProgressBar looks as in Figure 4.


Figure 4.

The following code adds a StatusBar to WPF using XAML.
  1. <StatusBar Name="SBar" Grid.Column="0" Grid.Row="5" VerticalAlignment="Bottom" Background="LightBlue"  >  
  2.     <StatusBarItem>  
  3.         <TextBlock>Status:</TextBlock>  
  4.     </StatusBarItem>  
  5. </StatusBar>  
The following code creates a ProgressBar dynamically and adds it to the StatusBar and sets the duration for changing the value of the ProgressBar.
  1. private void CreateDynamicProgressBarControl()  
  2. {  
  3.    ProgressBar PBar2 = new ProgressBar();  
  4.    PBar2.IsIndeterminate = false;  
  5.    PBar2.Orientation = Orientation.Horizontal;  
  6.    PBar2.Width = 200;  
  7.    PBar2.Height = 20;  
  8.    Duration duration = new Duration(TimeSpan.FromSeconds(20));  
  9.    DoubleAnimation doubleanimation = new DoubleAnimation(200.0, duration);  
  10.    PBar2.BeginAnimation(ProgressBar.ValueProperty, doubleanimation);  
  11.    SBar.Items.Add(PBar2);  
  12. }   
Summary

In this article, I discussed how to create and use a ProgressBar control available in WPF.

 

Up Next
    Ebook Download
    View all
    Learn
    View all