Over a million developers have joined DZone.

Customizing the ProgressBar control in WPF

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Once you get used to total UI makeover in WPF, you will see that often regular control styles don’t quite fit in the newly designed interface. In this article, I am going to talk about customizing a ProgressBar control in pure XAML and tracking its actions in the code-behind.

First of all, you need to place a regular ProgressBar control on your window or page. It is possible to create a control from scratch, but for this specific case, building on top of existing structures should suffice, since only the appearance (and not the behavior) is modified.

Once you’ve done that, your XAML code should look similar to this:

<Window x:Class="WPF_Test.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="300" Width="300" Loaded="Window_Loaded">    <Grid>        <ProgressBar Height="59" HorizontalAlignment="Left" Margin="10,10,0,0" Name="progressBar1" VerticalAlignment="Top" Width="256" Value="30" />    </Grid></Window>

Pretty simple so far. Now, I need to build a custom ControlTemplate that will be assigned to the existing ProgresBar control. Here is the XAML representation for it:

<ControlTemplate x:Key="myBar" TargetType="{x:Type ProgressBar}">    <Grid x:Name="myGrid">        <Rectangle x:Name="rectangle" HorizontalAlignment="Left">            <Rectangle.Fill>                <LinearGradientBrush EndPoint="-0.006,0.507" StartPoint="1.006,0.493">                    <GradientStop Color="Black" Offset="1"/>                    <GradientStop Color="#FFEC0B0B"/>                </LinearGradientBrush>            </Rectangle.Fill>        </Rectangle>    </Grid></ControlTemplate>

Inside the ControlTemplate I have a grid, that holds a rectangle, aligned to the left (since the progress is shown from left to right) and the rectangle has a gradient fill. This is pretty much it for now.

Now, I can easily assign the existing template to the ProgressBar control that is placed on my window:

<ProgressBar Template="{StaticResource myBar}" Height="59" HorizontalAlignment="Left" Margin="10,10,0,0" Name="progressBar1" VerticalAlignment="Top" Width="256" Value="30" />

But the progress bar could still look a bit “static”, especially when it is filling slowly, so I am going to add some gradient rotation to it. To do this, in the control template, I am going to create a <ControlTemplate.Resources> element and place a Storyboard inside, also using PointAnimationUsingKeyFrames to perform the gradient manipulations.

<ControlTemplate.Resources>   <Storyboard x:Key="Storyboard1" RepeatBehavior="Forever">       <PointAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(LinearGradientBrush.StartPoint)">           <SplinePointKeyFrame KeyTime="00:00:01" Value="1.006,0.493"/>           <SplinePointKeyFrame KeyTime="00:00:02" Value="-0.006,0.5"/>       </PointAnimationUsingKeyFrames>       <PointAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(Shape.Fill).(LinearGradientBrush.EndPoint)">           <SplinePointKeyFrame KeyTime="00:00:01" Value="-0.006,0.507"/>           <SplinePointKeyFrame KeyTime="00:00:02" Value="1.006,0.5"/>        </PointAnimationUsingKeyFrames>    </Storyboard></ControlTemplate.Resources>

I set the RepeatBehavior to Forever, so that the animation continues until the user decides to stop it (which by default shouldn’t happen). I am also manipulating keyframes, modifying the gradient start point and end point. I highly recommend experimenting with these values in Microsoft Expression Blend – it can make things a bit easier in this case. But basically, you have to use opposite values for each one of them, since those are going from one end to another.

Now to actually trigger the animation, in the code-behind I am using the following code:

Storyboard b = (Storyboard)progressBar1.Template.Resources["Storyboard1"];b.Begin(progressBar1, progressBar1.Template);

I am selecting the proper resource from the control template, assigned to the ProgressBar control and I start the animation. An important thing to mention here is that the control template should be passed to the Begin method in order to properly start rendering.

To set the value for the ProgressBar, you can ultimately change the default value, but since it is not bound to the width (mathematical calculations required), you can just modify the template properties in the code-behind:

Rectangle r = (Rectangle)progressBar1.Template.FindName("rectangle", progressBar1);r.Width = progressBar1.Value * progressBar1.Width / 100;

This will find the needed rectangle and assign a width value that is representing the current value.

The end result should look something like this:

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}