简体   繁体   中英

How to set foreground color of progress bar programatically?

I'm trying to change the foreground color of a progress bar dynaically in code but the usual assignment for setting the color isn't working for a progress bar.

This is how I'm trying to set the foreground color:

ProgressBar.Foreground = new SolidColorBrush(Colors.Green);

But the error I'm getting is:

an object reference is required for the nonstatic field method or property,'system.windows.controls.control.foreground.get'

This is the XAML declaration of the progress bar:

<ProgressBar  IsIndeterminate="False" x:Name="workProgressBar" Maximum="100" Foreground="BlueViolet" Margin="59,185,55,295" FontWeight="Bold" Style="{StaticResource ProgressBarStyle3}"/>

Does anyone know how to set the foreground color programmatically?

ProgressBar is the name of the class, rather than the name of your instance. Assuming that your progress bar has the name myProgressBar, the below should achieve what you want.

myProgressBar.Foreground = new SolidColorBrush(Colors.Green);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM