有人知道如何更改WPF-Progressbar的前景色。它似乎总是与绿色合并。
答案 0 :(得分:18)
试试这个
<ProgressBar Height="25" IsIndeterminate="True" Width="150" Foreground="Red" ></ProgressBar>
如果它不能按您的要求工作,则必须修改Progressbar的Style或ControlTemplate。
要做到这一点,您可以使用Microsoft的Expression Blend,或者您可以获得现有模板的副本并进行修改。
答案 1 :(得分:15)
不幸的是,它是默认样式的硬编码:
<Trigger Property="IsIndeterminate"
Value="false">
<Setter TargetName="Animation"
Property="Background"
Value="#80B5FFA9"/>
您可以从原始XAML创建自己的样式,或尝试覆盖Loaded事件中的背景,例如:
private void ProgressBar_Loaded(object sender, RoutedEventArgs e)
{
var p = (ProgressBar)sender;
p.ApplyTemplate();
((Panel)p.Template.FindName("Animation", p)).Background = Brushes.Red;
}
但它不可靠
答案 2 :(得分:5)
为什么不采取低阻力的路径并使用流行的MahApps库?
设置命名空间:xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
添加'MetroProgressBar'
<controls:MetroProgressBar Height="40"
Background="{StaticResource GrayBrush2}"
BorderBrush="{StaticResource GrayBrush8}"
BorderThickness="3"
Foreground="{StaticResource GrayBrush8}"
IsIndeterminate="False"
Value="{Binding CurrentProgressInfo.ProgressPercent}" />
将“前景”设置为您喜欢的颜色
答案 3 :(得分:3)
Marat Khasanov指出,不需要的绿色色调来自控件模板中名为“Animation”的对象。所以另一种简单的方法是隐藏该对象。这也将禁用动画“发光”效果,我认为这是一种资产,但您可能会将其视为交易杀手。我通过Loaded事件的处理程序实现了这个,如下所示。
这受到an answer to another question的启发。同样的警告适用:如果他们更改控件模板,那么这可能不再有效。
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;
}
答案 4 :(得分:1)
当Windows视觉设置进行了优化以获得最佳性能时,我遇到了类似的问题(控制面板 - &gt;系统 - &gt;高级系统设置 - &gt;高级 - &gt;性能 - &gt;设置 - &gt;视觉效果 - &gt;调整以获得最佳性能)。进度条在正常设置下看起来很好,但在“最佳性能”下可怕。我刚刚将ForeGround改为“LightGreen”。
这是我在正常条件下在默认ForeColor上看到的内容
这是我在调整以获得最佳性能时看到的内容
这是更改
//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"/>
这是我在调整后获得最佳性能后看到的内容
更多细节: http://justmycode.blogspot.com.au/2012/08/the-case-of-strangely-coloured.html
答案 5 :(得分:0)
我发现覆盖整个风格非常有用。 您可以使用
拉取任何控件的控件模板样式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);
进度样式(带有一些格式)如下所示:
<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>
答案 6 :(得分:0)
使用样式并根据您的要求进行自定义
</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>