c# - 如何更改 WPF 进度条上的颜色

标签 c# wpf progress-bar

我有一个 WPF、vista 风格的进度条,我想更改其上的画笔。我已经将前景画笔设置为另一种颜色,但是有一种嗖嗖的动画效果,其颜色仍然是默认的绿色。我该如何更改?

最佳答案

为此,您需要编辑项目中进度条控件的 ControlTemplate 样式。

<Style x:Key="{x:Type ProgressBar}"
     TargetType="{x:Type ProgressBar}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ProgressBar}">
        <Grid MinHeight="14" MinWidth="200">
          <Border 
            Name="PART_Track" 
            CornerRadius="2" 
            Background="{StaticResource PressedBrush}"
            BorderBrush="{StaticResource SolidBorderBrush}"
            BorderThickness="1" />
          <Border 
            Name="PART_Indicator" 
            CornerRadius="2" 
            Background="{StaticResource DarkBrush}" 
            BorderBrush="{StaticResource NormalBorderBrush}" 
            BorderThickness="1" 
            HorizontalAlignment="Left" />
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

使用这些样式的示例:

<LinearGradientBrush x:Key="PressedBrush" StartPoint="0,0" EndPoint="0,1">
  <GradientBrush.GradientStops>
    <GradientStopCollection>
      <GradientStop Color="#BBB" Offset="0.0"/>
      <GradientStop Color="#EEE" Offset="0.1"/>
      <GradientStop Color="#EEE" Offset="0.9"/>
      <GradientStop Color="#FFF" Offset="1.0"/>
    </GradientStopCollection>
  </GradientBrush.GradientStops>
</LinearGradientBrush>


...


<SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />


...


<LinearGradientBrush x:Key="DarkBrush" StartPoint="0,0" EndPoint="0,1">
  <GradientBrush.GradientStops>
    <GradientStopCollection>
      <GradientStop Color="#FFF" Offset="0.0"/>
      <GradientStop Color="#AAA" Offset="1.0"/>
    </GradientStopCollection>
  </GradientBrush.GradientStops>
</LinearGradientBrush>


...


<LinearGradientBrush x:Key="NormalBorderBrush" StartPoint="0,0" EndPoint="0,1">
  <GradientBrush.GradientStops>
    <GradientStopCollection>
      <GradientStop Color="#CCC" Offset="0.0"/>
      <GradientStop Color="#444" Offset="1.0"/>
    </GradientStopCollection>
  </GradientBrush.GradientStops>
</LinearGradientBrush>

您可以在 MSDN 上看到这样的示例:ProgressBar ControlTemplate Example

关于c# - 如何更改 WPF 进度条上的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2389953/

相关文章:

c# - 如果我使用 .Dispose();为什么我继续内存不足

c# - 从类中获取文本值的相同元素的不同结果

wpf - MVVM - 直接绑定(bind)到从 VM 公开的模型对象,或在 VM 中实现单独的属性来访问模型属性

python - Tkinter 中的进度条,里面有标签

c# - 如何使用 C# 文件 API 检查磁盘上的逻辑和物理文件大小

c# - 如何将命令绑定(bind)到按钮

c# - 使用 RenderedGeometry 作为路径数据不起作用

c# - 在 Parallel.ForEach 中监控进度

java - 在批处理文件运行时显示不确定的 JProgressBar

c# - Windows 服务自动停止