Does anybody know how to change the foreground color of a WPF-Progressbar. It always seems to be merged with green.
just try with this
<ProgressBar Height="25" IsIndeterminate="True" Width="150" Foreground="Red" ></ProgressBar>
If it is not working as you required you have to modify the Style or ControlTemplate of Progressbar.
To do that you can use Expression Blend from Microsoft or you can get a copy the existing template and modify it.
Unfortunately, it is hard coded in default style:
<Trigger Property="IsIndeterminate"
Value="false">
<Setter TargetName="Animation"
Property="Background"
Value="#80B5FFA9"/>
You can create your own style from original XAML or try to override background in the Loaded event for example:
private void ProgressBar_Loaded(object sender, RoutedEventArgs e)
{
var p = (ProgressBar)sender;
p.ApplyTemplate();
((Panel)p.Template.FindName("Animation", p)).Background = Brushes.Red;
}
but it's unreliable
Why not take a path of low resistance and use the popular MahApps library?
Setup the namespace: xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
Add the 'MetroProgressBar'
<controls:MetroProgressBar Height="40"
Background="{StaticResource GrayBrush2}"
BorderBrush="{StaticResource GrayBrush8}"
BorderThickness="3"
Foreground="{StaticResource GrayBrush8}"
IsIndeterminate="False"
Value="{Binding CurrentProgressInfo.ProgressPercent}" />
Set the 'Foreground' to your favorite color
Marat Khasanov pointed out that the unwanted green tint comes from the object named "Animation" within the control template. So another easy approach is to hide that object. This will also disable the animated "glow" effect, which I considered to be an asset but you might see as a deal-killer. I implemented this through a handler for the Loaded event as shown below.
This was inspired by an answer to another question. The same caveat applies: if they change the control template then this might no longer work.
public void ProgressBar_Loaded(object sender, RoutedEventArgs e)
{
var progressBar = sender as ProgressBar;
if (progressBar == null) return;
var animation = progressBar.Template.FindName("Animation", progressBar) as FrameworkElement;
if (animation != null)
animation.Visibility = Visibility.Collapsed;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With