Does anybody know how to change the foreground color of a WPF-Progressbar. It always seems to be merged with green.
问题:
回答1:
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
回答2:
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.
回答3:
Why not take a path of low resistance and use the popular MahApps library?
- Get the MahApps library: https://www.nuget.org/packages/MahApps.Metro
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
回答4:
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;
}
回答5:
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
Here's what I saw when adjusted for best performance
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
Some more detail: http://justmycode.blogspot.com.au/2012/08/the-case-of-strangely-coloured.html
回答6:
I found it rather useful to override the entire style. You can pull the control template styles of any control with
var yourcontrol = new ProgressBar();
// the control needs to load before it has a template.
yourcontrol.Loaded += (sender,e) => {
var str = new System.Text.StringBuilder();
using (var writer = new System.IO.StringWriter(str))
System.Windows.Markup.XamlWriter.Save(yourcontrol .Template, writer);
System.Diagnostics.Debug.Write(str);
};
// add it to your main grid, or some control thats loaded on screen.
gridMain.Children.Add(yourcontrol);
The progress style (with some formatting) comes out as the following:
<ControlTemplate x:Key="templateprogress"
TargetType="ProgressBar">
<Grid Name="TemplateRoot"
SnapsToDevicePixels="True">
<Grid.Resources>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1"
x:Key="brushMagic1">
<GradientStop Color="#50FFFFFF"
Offset="0.5385" />
<GradientStop Color="#00FFFFFF"
Offset="0.5385" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1"
x:Key="brushMagic2">
<GradientStop Color="#80FFFFFF"
Offset="0.05" />
<GradientStop Color="#00FFFFFF"
Offset="0.25" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0,0"
EndPoint="1,0"
x:Key="brushGlowRect">
<GradientStop Color="#00FFFFFF"
Offset="0" />
<GradientStop Color="#60FFFFFF"
Offset="0.4" />
<GradientStop Color="#60FFFFFF"
Offset="0.6" />
<GradientStop Color="#00FFFFFF"
Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0,0"
EndPoint="1,0"
x:Key="brushLeftDark">
<GradientStop Color="#0C000000"
Offset="0" />
<GradientStop Color="#20000000"
Offset="0.3" />
<GradientStop Color="#00000000"
Offset="1" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0,0"
EndPoint="1,0"
x:Key="brushRightDark">
<GradientStop Color="#00000000"
Offset="0" />
<GradientStop Color="#20000000"
Offset="0.7" />
<GradientStop Color="#0C000000"
Offset="1" />
</LinearGradientBrush>
<RadialGradientBrush RadiusX="1"
RadiusY="1"
x:Key="brushRadialLeftLight"
RelativeTransform="1,0,0,1,0.5,0.5">
<GradientStop Color="#60FFFFC4"
Offset="0" />
<GradientStop Color="#00FFFFC4"
Offset="1" />
</RadialGradientBrush>
<LinearGradientBrush StartPoint="0,1"
EndPoint="0,0"
x:Key="brushCenterLight">
<GradientStop Color="#60FFFFC4"
Offset="0" />
<GradientStop Color="#00FFFFC4"
Offset="1" />
</LinearGradientBrush>
<RadialGradientBrush RadiusX="1"
RadiusY="1"
x:Key="brushRadial1"
RelativeTransform="1,0,0,1,-0.5,0.5">
<GradientStop Color="#60FFFFC4"
Offset="0" />
<GradientStop Color="#00FFFFC4"
Offset="1" />
</RadialGradientBrush>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1"
x:Key="brushHighlight1">
<GradientStop Color="#90FFFFFF"
Offset="0.5385" />
<GradientStop Color="#00FFFFFF"
Offset="0.5385" />
</LinearGradientBrush>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1"
x:Key="brushHighlight2">
<GradientStop Color="#80FFFFFF"
Offset="0.05" />
<GradientStop Color="#00FFFFFF"
Offset="0.25" />
</LinearGradientBrush>
</Grid.Resources>
<Rectangle RadiusX="2"
RadiusY="2"
Fill="{TemplateBinding Panel.Background}" />
<Border CornerRadius="2,2,2,2"
Margin="1,1,1,1"
Background="{StaticResource ResourceKey=brushMagic1}" />
<Border BorderThickness="1,0,1,1"
BorderBrush="#80FFFFFF"
Margin="1,1,1,1"
Background="{StaticResource ResourceKey=brushMagic2}" />
<Rectangle Name="PART_Track"
Margin="1,1,1,1" />
<Decorator Name="PART_Indicator"
Margin="1,1,1,1"
HorizontalAlignment="Left">
<Grid Name="Foreground">
<Rectangle Fill="{TemplateBinding TextElement.Foreground}"
Name="Indicator" />
<Grid Name="Animation"
ClipToBounds="True">
<Rectangle Name="PART_GlowRect"
Width="100"
Margin="-100,0,0,0"
HorizontalAlignment="Left"
Fill="{StaticResource ResourceKey=brushGlowRect}" />
</Grid>
<Grid Name="Overlay">
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="15" />
<ColumnDefinition Width="0.1*" />
<ColumnDefinition MaxWidth="15" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Rectangle RadiusX="1"
RadiusY="1"
Name="LeftDark"
Margin="1,1,0,1"
Grid.RowSpan="2"
Fill="{StaticResource ResourceKey=brushLeftDark}" />
<Rectangle RadiusX="1"
RadiusY="1"
Name="RightDark"
Margin="0,1,1,1"
Grid.Column="2"
Grid.RowSpan="2"
Fill="{StaticResource ResourceKey=brushRightDark}" />
<Rectangle Name="LeftLight"
Grid.Column="0"
Grid.Row="2"
Fill="{StaticResource ResourceKey=brushRadialLeftLight}" />
<Rectangle Name="CenterLight"
Grid.Column="1"
Grid.Row="2"
Fill="{StaticResource ResourceKey=brushCenterLight}" />
<Rectangle Name="RightLight"
Grid.Column="2"
Grid.Row="2"
Fill="{StaticResource ResourceKey=brushRadial1}" />
<Border Name="Highlight1"
Grid.ColumnSpan="3"
Grid.RowSpan="2"
Background="{StaticResource ResourceKey=brushHighlight1}" />
<Border Name="Highlight2"
Grid.ColumnSpan="3"
Grid.RowSpan="2"
Background="{StaticResource ResourceKey=brushHighlight2}" />
</Grid>
</Grid>
</Decorator>
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
CornerRadius="2,2,2,2"
BorderBrush="{TemplateBinding Border.BorderBrush}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ProgressBar.Orientation"
Value="Vertical">
<Setter Property="FrameworkElement.LayoutTransform"
TargetName="TemplateRoot">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="ProgressBar.IsIndeterminate"
Value="True">
<Setter Property="UIElement.Visibility"
TargetName="LeftDark"
Value="Collapsed" />
<Setter Property="UIElement.Visibility"
TargetName="RightDark"
Value="Collapsed" />
<Setter Property="UIElement.Visibility"
TargetName="LeftLight"
Value="Collapsed" />
<Setter Property="UIElement.Visibility"
TargetName="CenterLight"
Value="Collapsed" />
<Setter Property="UIElement.Visibility"
TargetName="RightLight"
Value="Collapsed" />
<Setter Property="UIElement.Visibility"
TargetName="Indicator"
Value="Collapsed" />
</Trigger>
<Trigger Property="ProgressBar.IsIndeterminate"
Value="False">
<Setter Property="Panel.Background"
TargetName="Animation"
Value="#80B5FFA9" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style TargetType="ProgressBar">
<Setter Property="Template"
Value="{StaticResource ResourceKey=templateprogress}" />
</Style>
回答7:
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>