有没有办法通过绑定到视图模型属性来更改 WPF 进度条的颜色 [英] Is there a way to change the color of a WPF progress bar via binding to a view model property
本文介绍了有没有办法通过绑定到视图模型属性来更改 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
这篇关于有没有办法通过绑定到视图模型属性来更改 WPF 进度条的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文