Progressbar foreground color

2019-01-09 08:57发布

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

7条回答
放荡不羁爱自由
2楼-- · 2019-01-09 09:28

Use style and customize as per yours requirement

                        </Border>

                    </DockPanel>
                </Border>
                    <Border Background="White" Margin="40,0,0,0" Width="1.5" HorizontalAlignment="Left"></Border>
                    <Border Background="White" Margin="80,0,0,0" Width="1.5" HorizontalAlignment="Left"></Border>
                    <Border Background="White" Margin="120,0,0,0" Width="1.5" HorizontalAlignment="Left"></Border>
                    <Border Background="White" Margin="160,0,0,0" Width="1.5" HorizontalAlignment="Left"></Border>
                </Grid>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
查看更多
再贱就再见
3楼-- · 2019-01-09 09:29

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.

查看更多
贪生不怕死
4楼-- · 2019-01-09 09:33

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

查看更多
做个烂人
5楼-- · 2019-01-09 09:35

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

查看更多
6楼-- · 2019-01-09 09:39

I came across a similar issue when the Windows visual settings were optimised for best performance (Control Panel -> System -> Advanced System Settings -> Advanced -> Performance -> Settings -> Visual Effects -> Adjust for best performance). The progress bar looked fine under normal settings, but horrible under "best performance". I just changed ForeGround to "LightGreen".

Here's what I saw on default ForeColor under normal conditions

enter image description here

Here's what I saw when adjusted for best performance

enter image description here

Here is the change

//before
<ProgressBar Name="Progress" Grid.Column="0" Value="{Binding ProgressValue}" HorizontalAlignment="Stretch"/>

//after
<ProgressBar Foreground="LightGreen" Name="Progress" Grid.Column="0" Value="{Binding ProgressValue}" HorizontalAlignment="Stretch"/>

Here's what I saw after when adjusted for best performance

enter image description here

Some more detail: http://justmycode.blogspot.com.au/2012/08/the-case-of-strangely-coloured.html

查看更多
该账号已被封号
7楼-- · 2019-01-09 09:41

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;

    }
查看更多
登录 后发表回答