如何在不失去样式的情况下更改WPF中进度条的颜色?

时间:2015-10-28 09:35:28

标签: wpf progress-bar

我想更改WPF中进度条的颜色。当我改变颜色时,它会失去它的风格。如何改变颜色保持窗口默认样式?

1 个答案:

答案 0 :(得分:1)

  1. 覆盖进度条的控件模板
  2. 这会生成XAML代码,如:

    <ProgressBar.Resources>
              <ControlTemplate x:Key="ProgressBarControlTemplate1" TargetType="{x:Type ProgressBar}">
                <Grid x:Name="TemplateRoot" SnapsToDevicePixels="True">
                  <Rectangle Fill="{TemplateBinding Background}" RadiusY="2" RadiusX="2"/>
              <Border CornerRadius="2" Margin="1">
                <Border.Background>
                  <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#50FFFFFF" Offset="0.5385"/>
                    <GradientStop Color="Transparent" Offset="0.5385"/>
                  </LinearGradientBrush>
                </Border.Background>
              </Border>
              <Border BorderBrush="#80FFFFFF" BorderThickness="1,0,1,1" Margin="1">
                <Border.Background>
                  <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#80FFFFFF" Offset="0.05"/>
                    <GradientStop Color="Transparent" Offset="0.25"/>
                  </LinearGradientBrush>
                </Border.Background>
              </Border>
              <Rectangle x:Name="PART_Track" Margin="1"/>
              <Decorator x:Name="PART_Indicator" HorizontalAlignment="Left" Margin="1">
                <Grid x:Name="Foreground">
                  <Rectangle x:Name="Indicator" Fill="{TemplateBinding Foreground}"/>
                  <Grid x:Name="Animation" ClipToBounds="True">
                    <Rectangle x:Name="PART_GlowRect" HorizontalAlignment="Left" Margin="-100,0,0,0" Width="100">
                      <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
                          <GradientStop Color="Transparent" Offset="0"/>
                          <GradientStop Color="#60FFFFFF" Offset="0.4"/>
                          <GradientStop Color="#60FFFFFF" Offset="0.6"/>
                          <GradientStop Color="Transparent" Offset="1"/>
                        </LinearGradientBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                  </Grid>
                  <Grid x:Name="Overlay">
                    <Grid.ColumnDefinitions>
                      <ColumnDefinition MaxWidth="15"/>
                      <ColumnDefinition Width="0.1*"/>
                      <ColumnDefinition MaxWidth="15"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                      <RowDefinition/>
                      <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Rectangle x:Name="LeftDark" Margin="1,1,0,1" RadiusY="1" RadiusX="1" Grid.RowSpan="2">
                      <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
                          <GradientStop Color="#0C000000" Offset="0"/>
                          <GradientStop Color="#20000000" Offset="0.3"/>
                          <GradientStop Color="#00000000" Offset="1"/>                          
                        </LinearGradientBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle x:Name="RightDark" Grid.Column="2" Margin="0,1,1,1" RadiusY="1" RadiusX="1" Grid.RowSpan="2">
                      <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
                          <GradientStop Color="#00000000" Offset="0"/>
                          <GradientStop Color="#20000000" Offset="0.7"/>
                          <GradientStop Color="#0C000000" Offset="1"/>
                        </LinearGradientBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle x:Name="LeftLight" Grid.Column="0" Grid.Row="2">
                      <Rectangle.Fill>
                        <RadialGradientBrush RadiusY="1" RadiusX="1">
                          <RadialGradientBrush.RelativeTransform>
                            <MatrixTransform Matrix="1,0,0,1,0.5,0.5"/>
                          </RadialGradientBrush.RelativeTransform>
                          <GradientStop Color="#60FFFFC4" Offset="0"/>
                          <GradientStop Color="#00FFFFC4" Offset="1"/>
                        </RadialGradientBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle x:Name="CenterLight" Grid.Column="1" Grid.Row="2">
                      <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="0,0" StartPoint="0,1">
                          <GradientStop Color="#60FFFFC4" Offset="0"/>
                          <GradientStop Color="#00FFFFC4" Offset="1"/>
                        </LinearGradientBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle x:Name="RightLight" Grid.Column="2" Grid.Row="2">
                      <Rectangle.Fill>
                        <RadialGradientBrush RadiusY="1" RadiusX="1">
                          <RadialGradientBrush.RelativeTransform>
                            <MatrixTransform Matrix="1,0,0,1,-0.5,0.5"/>
                          </RadialGradientBrush.RelativeTransform>
                          <GradientStop Color="#60FFFFC4" Offset="0"/>
                          <GradientStop Color="#00FFFFC4" Offset="1"/>
                        </RadialGradientBrush>
                      </Rectangle.Fill>
                    </Rectangle>
                    <Border x:Name="Highlight1" Grid.ColumnSpan="3" Grid.RowSpan="2">
                      <Border.Background>
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                          <GradientStop Color="#90FFFFFF" Offset="0.5385"/>
                          <GradientStop Color="Transparent" Offset="0.5385"/>
                        </LinearGradientBrush>
                      </Border.Background>
                    </Border>
                    <Border x:Name="Highlight2" Grid.ColumnSpan="3" Grid.RowSpan="2">
                      <Border.Background>
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                          <GradientStop Color="#80FFFFFF" Offset="0.05"/>
                          <GradientStop Color="Transparent" Offset="0.25"/>
                        </LinearGradientBrush>
                      </Border.Background>
                    </Border>
                  </Grid>
                </Grid>
              </Decorator>
              <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/>
            </Grid>
            <ControlTemplate.Triggers>
              <Trigger Property="Orientation" Value="Vertical">
                <Setter Property="LayoutTransform" TargetName="TemplateRoot">
                  <Setter.Value>
                    <RotateTransform Angle="-90"/>
                  </Setter.Value>
                </Setter>
              </Trigger>
              <Trigger Property="IsIndeterminate" Value="True">
                <Setter Property="Visibility" TargetName="LeftDark" Value="Collapsed"/>
                <Setter Property="Visibility" TargetName="RightDark" Value="Collapsed"/>
                <Setter Property="Visibility" TargetName="LeftLight" Value="Collapsed"/>
                <Setter Property="Visibility" TargetName="CenterLight" Value="Collapsed"/>
                <Setter Property="Visibility" TargetName="RightLight" Value="Collapsed"/>
                <Setter Property="Visibility" TargetName="Indicator" Value="Collapsed"/>
              </Trigger>
              <!--<Trigger Property="IsIndeterminate" Value="False">
                <Setter Property="Background" TargetName="Animation" Value="#80B5FFA9"/>
              </Trigger>-->
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </ProgressBar.Resources>
    
    1. 如果您需要更改默认值,请执行模板中的更改

    2. 定义数据触发器并使用setter更改“前景”属性