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

发布于 2024-09-15 10:54:50 字数 90 浏览 3 评论 0原文

我想要一个进度条根据当前值当前所在的范围来更改其颜色。我想知道进度条上是否有一个属性,我可以将视图模型属性绑定到该属性来更改颜色。 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞄了个咪的 2024-09-22 10:54:50

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

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

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

但是别担心,这很简单:

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

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
﹎☆浅夏丿初晴 2024-09-22 10:54:50

您是否尝试更改进度条的整个颜色,或者是否尝试根据值使进度条的不同部分具有不同的颜色?如果是后者,您需要渐变画笔,根据进度栏中的值设置不同的渐变停止点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文