Is there a way to change the color of a WPF progress bar via binding to a view model property

只谈情不闲聊 提交于 2019-12-24 00:56:19

问题


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.



来源:https://stackoverflow.com/questions/3514662/is-there-a-way-to-change-the-color-of-a-wpf-progress-bar-via-binding-to-a-view-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!