DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • How to Convert Between PDF and TIFF in Java
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • Key Considerations in Cross-Model Migration

Customizing the ProgressBar control in WPF

By 
Denzel D. user avatar
Denzel D.
·
May. 18, 10 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
40.4K Views

Join the DZone community and get the full member experience.

Join For Free

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:


Windows Presentation Foundation

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!