Solved

Adding text to a progress bar in WPF

Posted on 2014-09-13
5
667 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now