进度条前景色

Mor*_*der 23 c# wpf progress-bar

有谁知道如何更改WPF-Progressbar的前景色.它似乎总是与绿色合并.

Kis*_*mar 18

试试看吧

   <ProgressBar Height="25" IsIndeterminate="True" Width="150" Foreground="Red" ></ProgressBar>
Run Code Online (Sandbox Code Playgroud)

如果它不能按您的要求工作,则必须修改Progressbar的Style或ControlTemplate.

要做到这一点,您可以使用Microsoft的Expression Blend,或者您可以获得现有模板的副本并进行修改.


Mar*_*nov 15

不幸的是,它是默认样式的硬编码:

<Trigger Property="IsIndeterminate"
     Value="false">
<Setter TargetName="Animation"
    Property="Background"
    Value="#80B5FFA9"/>
Run Code Online (Sandbox Code Playgroud)

您可以从原始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;
}
Run Code Online (Sandbox Code Playgroud)

但它不可靠

  • 此解决方案适用于Aero主题.您应该检查其他主题以确保名为Animation的元素存在于那里.当然,自定义模板可能会出现此问题.另一个麻烦是主题切换.我不知道当用户更改操作系统主题时WPF是否重新加载控件.如果没有,那么你需要处理这个时刻并再次改变动画的背景.如果您不想提供自己的模板,我认为从ProgressBar派生会很好.在模板更改时,您应该重写OnApplyTemplate以解决模板错误. (2认同)

Gor*_*ysz 5

为什么不采取低阻力的路径并使用流行的MahApps库?

  1. 获取MahApps库:https://www.nuget.org/packages/MahApps.Metro
  2. 设置命名空间: xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"

  3. 添加'MetroProgressBar'

                    <controls:MetroProgressBar Height="40"
                                           Background="{StaticResource GrayBrush2}"
                                           BorderBrush="{StaticResource GrayBrush8}"
                                           BorderThickness="3"
                                           Foreground="{StaticResource GrayBrush8}"
                                           IsIndeterminate="False"
                                           Value="{Binding CurrentProgressInfo.ProgressPercent}" />
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将"前景"设置为您喜欢的颜色