Is there a way to change the color of a WPF progre

2019-07-18 04:03发布

问题:

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:

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


回答2:

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.