有没有办法通过绑定到视图模型属性来更改 WPF 进度条的颜色

有没有办法通过绑定到视图模型属性来更改 WPF 进度条的颜色

问题描述:

我想要一个进度条根据当前值当前所处的范围来改变它的颜色.我想知道进度条上是否有一个属性可以绑定一个视图模型属性来改变颜色.WPF进度条上是否存在这样的属性?

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?

只需将前景色更改为您喜欢的颜色即可:

Just change the foreground color to the color you like:

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

编辑(回答你的评论):是的,你需要一个 Brush 属性(几乎所有的颜色属性都是在 WPF 中刷过的)

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

和属性:

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