views:

49

answers:

2

I'm wanting a progress bar to change it's color depending on the range the current value is currently in. I was wondering if there was an attribute on the progress bar that I could bind a view model property to to change the color. Does such an attribute exist on the WPF progressbar?

+1  A: 

Just change the foreground color to the color you like:

<ProgressBar Foreground="{Binding PBarColorBrush}" Value="{Binding PBarValue}" />

Edit (answering your comment): Yes, you need a Brush property (Almost all color properties are Brushed in WPF)

But don't worry it's very simple:

Public Sub DoWork()
    For i = 1 To 100
        If i < 50 Then
            PBarColorBrush = Brushes.Blue
        ElseIf i < 80 Then
            PBarColorBrush = Brushes.Green
        Else
            PBarColorBrush = Brushes.Red
        End If
    Next

End Sub

And the property:

Private _PBarColorBrush As Brush
Public Property PBarColorBrush() As Brush
    Get
        Return _PBarColorBrush
    End Get
    Set(ByVal value As Brush)
        _PBarColorBrush = value
        OnPropertyChanged("PBarColorBrush")
    End Set
End Property
Eduardo Molteni
So bind Foreground to a System.Drawing.Color property?
BrianP
In other words, in your example "PBarColorBruch" is a Color property?
BrianP
You'd either expose a `Brush` property (not a color) or, if this is something you'd be using a lot, build an `int`-to-`Brush` converter.
Robert Rossney
@BrianP: I've edited the answer with more details
Eduardo Molteni
This absolutely works (Many Thanks!!!!). However there must be some other properties on the progressbar control that I have to bind to, or explicitly set in my xaml because when I set the color in the view model property, the color isn't exact in the view (e.g. yellow doesn't exactly look yellow, and so forth) because there is some other styling causing the bar itself to have a slight white animation as well as some shading. How can I get rid of that stuff?
BrianP
Actually, no matter what I set the Foreground attribute to, they are different shades of green. There's definitely another attribute that I have to explicitly set to fix that to make it true colors on the bar.
BrianP
Yes you can, but you have to change the control style. See this question as sample http://stackoverflow.com/questions/2389953/how-do-you-change-the-colors-on-a-wpf-progressbar
Eduardo Molteni
Yep, that's the conclusion that I can to in my research. Thanks for the answer!
BrianP
A: 

Are you trying to change the entire color of the progress bar or are you trying to have different parts of the progress bar be different colors, based on the value? If the latter you'll want a gradient brush, setting different gradient stops according to the values in the progress bar.

Peter Nelson
The entire color of the progress bar
BrianP