Вы можете привязать свойство Foreground
ProgressBar к его свойству Value
, используя преобразователь значений , который преобразует из double
в Brush
, как показано в примере ниже. Обратите внимание, что для тестирования свойство ProgressBar Value
также связано, в частности со свойством Value
элемента управления Slider.
<Window.Resources>
<local:ProgressForegroundConverter x:Key="ProgressForegroundConverter"/>
</Window.Resources>
<StackPanel>
<ProgressBar Margin="10"
Value="{Binding ElementName=progress, Path=Value}"
Foreground="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Value, Converter={StaticResource ProgressForegroundConverter}}"/>
<Slider Name="progress" Margin="10" Minimum="0" Maximum="100"/>
</StackPanel>
Преобразователь значений привязки может выглядеть следующим образом:
public class ProgressForegroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double progress = (double)value;
Brush foreground = Brushes.Green;
if (progress >= 90d)
{
foreground = Brushes.Red;
}
else if (progress >= 60d)
{
foreground = Brushes.Yellow;
}
return foreground;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}