Solved

Adding text to a progress bar in WPF

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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…

840 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