'How to change the progressbar foreground color based on its value depending on the range the current value is currently in

i have already checked the below question but i didnt get completely as i am new to WPF. Is there a way to change the color of a WPF progress bar via binding to a view model property

If you have any sample please provide me.



Solution 1:[1]

Clemens solution does not work. To fix the blank color issue, in the Converter code return something like:

return   new SolidColorBrush (Colors.Red);

as opposed to Bushes.Red

Solution 2:[2]

I wanted to take the time to add to this because the answer didn't directly work for me so wanted to provide some clarity if anyone else needed it.

Firstly, when you create the ProgressForegroundConverter class, it needs to either be in the same namespace as your window, or you need to add a local reference in your Window xaml declaration. In my instance, the class was in the same folder as my ViewModel, not the window:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Syncfusion="http://schemas.syncfusion.com/wpf" x:Class="Rota_Analysis.MainWindow"
        mc:Ignorable="d"
        xmlns:local="clr-namespace:Rota_Analysis.ViewModels"
        Title="Rota Analysis" 
        Height="800" Width="800" 
        Icon="/icon.png"
        DataContext="local:ViewModel" FontWeight="Normal" FontSize="14">

You'll see above the declaration of xmlns:local for the ViewModel namespace:

xmlns:local="clr-namespace:Rota_Analysis.ViewModels"

Next, I needed to declare the ProgressForegroundConverter in Window.Resources:

<Window.Resources>
        <local:ProgressForegroundConverter :Key="ProgressForegroundConverter"/>
</Window.Resources>

As @Mark Bidar righly says, the original example only returns black as a colour so the class needs some modification to return a type of SolidColorBrush:

public class ProgressForegroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double progress = (double)value;
            SolidColorBrush foreground = new SolidColorBrush(Colors.Green);

            if (progress >= 0.75)
            {
                foreground = new SolidColorBrush(Colors.Red);
            }
            else if (progress >= 0.55)
            {
                foreground = new SolidColorBrush(Colors.Orange);
            }
            else if (progress < 0.55)
            {
                foreground = new SolidColorBrush(Colors.Green);
            }

            return foreground;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Then, finally, your progress bar xaml:

<ProgressBar x:Name="progressBar" Background="Transparent" Visibility="Visible" Minimum="0" Maximum="1" Value="{Binding Path=CapacityThreshold}" 
                                                         Foreground="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Value, Converter={StaticResource ProgressForegroundConverter}}" />

In this implementation I'm binding the value to a property on my ViewModel called CapacityThreshold. The binding for the progressbar forcolour then looks at the value of itself and passes it to the ProgressForegroundConverter that gives the colour you want to set from the class.

Whilst the answers above got me to where I needed to be, I just thought someone else may benefit from a clearer answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephen Rauch
Solution 2 paulpitchford