Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progressbar foreground color

Does anybody know how to change the foreground color of a WPF-Progressbar. It always seems to be merged with green.

like image 670
Morvader Avatar asked Jan 19 '11 11:01

Morvader


4 Answers

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.

like image 195
Kishore Kumar Avatar answered Nov 16 '22 10:11

Kishore Kumar


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

like image 34
Marat Khasanov Avatar answered Nov 16 '22 08:11

Marat Khasanov


Why not take a path of low resistance and use the popular MahApps library?

  1. Get the MahApps library: https://www.nuget.org/packages/MahApps.Metro
  2. Setup the namespace: xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"

  3. Add the 'MetroProgressBar'

                    <controls:MetroProgressBar Height="40"
                                           Background="{StaticResource GrayBrush2}"
                                           BorderBrush="{StaticResource GrayBrush8}"
                                           BorderThickness="3"
                                           Foreground="{StaticResource GrayBrush8}"
                                           IsIndeterminate="False"
                                           Value="{Binding CurrentProgressInfo.ProgressPercent}" />
    
  4. Set the 'Foreground' to your favorite color

like image 4
Gordon Slysz Avatar answered Nov 16 '22 09:11

Gordon Slysz


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;

    }
like image 3
Tony Pulokas Avatar answered Nov 16 '22 10:11

Tony Pulokas