Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding text to a progress bar in WPF

Posted on 2014-09-13
5
Medium Priority
?
1,178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

722 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