Solved

Adding text to a progress bar in WPF

Posted on 2014-09-13
5
754 Views
Last Modified: 2014-09-28
Okay, so I know there are a few answers to this question online in other places, but I'm not satisfied with them.
I'm trying to do something that seems simple enough, I want to add text to the System.Windows.Controls.ProgressBar control.

I know I can add a TextBlock on top of the ProgressBar for a quick fix, but I want this to be reusable (I use it on almost every form).
I've also created a UserControl the holds a ProgressBar with a TextBlock on top, but I'm then forced to reference every property like this:

ProgressBarWithText.InnerProgressBar.Value = 2
ProgressBarWithText.InnterTextBlock.Text = "Some Text Here"

Open in new window


I want to be able to access the properties of the ProgressBar as I normally would, with just the addition of a ProgressBar.Text property so that I can add text to it. I thought I should be able to do it by inheriting like so:

Public Class ProgressBarWithText
    Inherits ProgressBar

   
End Class

Open in new window


I added a DependencyProperty for "Text", but I don't know how to go about adding it to the control. I though about adding a TextBlock to the control, but I'm not sure how to do that. When I looked into it, I discovered I would probably have to modify the ControlTemplate to include a TextBlock and I have to clue how to  do that.

I won't ask for a simple way to accomplish this, but is there any way to do it at all? Whether I have to modify the ControlTemplate or not, I'd like to get this working.
0
Comment
Question by:BROOKLYN1950
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:MikeToole
ID: 40334314
The User-Control approach is the easiest way to approach this, just create Dependency Properties for Value and Text in the control's code behind and use them to provide values to the wrapped controls. The Dependency properties are then available to users of the new control via either code or xaml.

The User Control would look something like this...

<UserControl x:Class="WpfSandpit.ProgressBarWithText"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Name="This">
    <Grid>
        <ProgressBar Value="{Binding Value,ElementName=This}" VerticalAlignment="Center" Height="20"/>
        <TextBlock Text="{Binding Text,ElementName=This}"  VerticalAlignment="Center" Height="20"/>            
    </Grid>
</UserControl>
[code]

the DPs are defined in code-behind - here's the definition of the [i]Text  [/i] DP ...
[code]
        public ProgressBarWithText()
        {
            InitializeComponent();
        }


        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ProgressBarWithText), new PropertyMetadata(""));

Open in new window


The new control is then used like this (where the Text property can be bound to any source, or set in code) ...
       
      <local:ProgressBarWithText Text="My Text"/>  

Open in new window

0
 

Author Comment

by:BROOKLYN1950
ID: 40334546
Hi,

I have tried this before and it does work, but I'm trying to find a way of doing this which avoids me having to recreate each of the properties I want to use. For example I also use MinValue, MaxValue and IsIndeterminate, so I'd have to create dependency properties for each of those, plus any other properties I felt like using.
I would like to find some other way if possible, but if not this does seem like the best option.
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 40338567
I believe that the only other option, as you've already considered, is to re-template the control - I've never done that myself, but from the descriptions I've come across it don't sound too difficult.
0
 

Author Closing Comment

by:BROOKLYN1950
ID: 40348864
In the end I had to learn how to re-template a control anyway, so I ended up inheriting PorgressBar like I wanted and adding a TextBlock to the template to display the value of the Text property.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question