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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Create a WPF Application

Trending

  • Exploring the Evolution and Impact of Computer Networks
  • Architecting a Completely Private VPC Network and Automating the Deployment
  • Helm Dry Run: Guide and Best Practices
  • Log Analysis: How to Digest 15 Billion Logs Per Day and Keep Big Queries Within 1 Second

Customizing the ProgressBar control in WPF

Denzel D. user avatar by
Denzel D.
·
May. 18, 10 · Tutorial
Like (0)
Save
Tweet
Share
38.94K 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.

Related

  • Create a WPF Application

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: