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?
views:
49answers:
2
+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
2010-08-18 18:03:37
So bind Foreground to a System.Drawing.Color property?
BrianP
2010-08-18 18:41:44
In other words, in your example "PBarColorBruch" is a Color property?
BrianP
2010-08-18 18:42:34
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
2010-08-18 18:54:21
@BrianP: I've edited the answer with more details
Eduardo Molteni
2010-08-19 12:42:05
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
2010-08-19 18:02:06
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
2010-08-19 18:10:27
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
2010-08-19 18:30:39
Yep, that's the conclusion that I can to in my research. Thanks for the answer!
BrianP
2010-08-19 19:53:57
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
2010-08-19 02:25:34