Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding text to a progress bar in WPF

Posted on 2014-09-13
5
Medium Priority
?
1,325 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 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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, …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

885 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