进度条颜色错误

时间:2019-07-04 14:06:04

标签: c# wpf

我有一个进度条,开始时用绿色确定,然后改变行为,变成红色且不确定,最后在最后一步返回确定。
在第二步中,红色是正确的,但在最后一步中,前景色是错误的,它是棕色而不是红色。

Progress bar with wrong color

xaml文件中的进度栏​​:

<Grid Margin="10,0" Height="58" VerticalAlignment="Bottom">
    <ProgressBar x:Name="progress_bar" Height="20" VerticalAlignment="Bottom" Margin="0,0,0,38" />
    <TextBlock x:Name="progress_bar_text" Margin="300,2,300,40" HorizontalAlignment="Center" >0% - Waiting to start...</TextBlock>
</Grid>

然后在按下中止按钮时:

private void Button_abort_Click(object sender, RoutedEventArgs e)
{       
    progress_bar.IsIndeterminate = true;
    progress_bar.Foreground = Brushes.Red;        
}

最后,当任务终止时,任务栏的行为将返回确定。

progress_bar.IsIndeterminate = false;
progress_bar.Foreground = Brushes.Red;

为什么进度条是棕色而不是红色?

1 个答案:

答案 0 :(得分:0)

尝试在您的代码中添加此样式:

<Style TargetType="{x:Type ProgressBar}">
    <Setter Property="Foreground" Value="HotPink"/>
    <Setter Property="BorderBrush" Value="Gray"/>
    <Setter Property="Value" Value="10"/>
    <Setter Property="Minimum" Value="0"/>
    <Setter Property="Maximum" Value="100"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ProgressBar}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"  Width="{TemplateBinding Width}" MinWidth="10" Height="{TemplateBinding Height}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
                    <Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill">
                        <Grid Width="{TemplateBinding Maximum}" Height="50">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid Background="{TemplateBinding Foreground}" Grid.Column="0" Width="{TemplateBinding Value}">
                            </Grid>
                        </Grid>
                    </Viewbox>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这将覆盖原始控件模板并使进度条变平,并且颜色将保持您设置的原样