wpf 进度列前景色彩

vmdwslir  于 2022-11-18  发布在  其他
关注(0)|答案(8)|浏览(157)

有没有人知道如何改变WPF进度条的前景色。它似乎总是与绿色合并。

kt06eoxx

kt06eoxx1#

试试这个

<ProgressBar Height="25" IsIndeterminate="True" Width="150" Foreground="Red" ></ProgressBar>

如果它不能按您的要求工作,您必须修改Progressbar的Style或ControlTemplate。
要执行此操作,您可以使用Microsoft的Expression Blend,也可以获取现有模板的副本并对其进行修改。

n7taea2i

n7taea2i2#

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

<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;
}

但不可靠

9gm1akwq

9gm1akwq3#

为什么不走一条阻力小的路,使用流行的MahApps库呢?
1.获取MahApps库:https://www.nuget.org/packages/MahApps.Metro
1.设置命名空间:xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
1.添加“地铁进度条”

<controls:MetroProgressBar Height="40"
                                       Background="{StaticResource GrayBrush2}"
                                       BorderBrush="{StaticResource GrayBrush8}"
                                       BorderThickness="3"
                                       Foreground="{StaticResource GrayBrush8}"
                                       IsIndeterminate="False"
                                       Value="{Binding CurrentProgressInfo.ProgressPercent}" />

1.将“前景”设置为您喜爱的颜色

nszi6y05

nszi6y054#

马拉特Khasanov指出,不需要的绿色色调来自控件模板中名为“Animation”的对象。因此,另一个简单的方法是隐藏该对象。这也将禁用动画“glow”效果,我认为这是一项资产,但您可能会认为这是一个交易杀手。我通过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;

    }
4ngedf3f

4ngedf3f5#

当Windows视觉设置为最佳性能进行优化时,我遇到了类似的问题(控制面板-〉系统-〉高级系统设置-〉高级-〉性能-〉设置-〉视觉效果-〉调整为最佳性能)。进度条在正常设置下看起来很好,但在“最佳性能”下很糟糕。我刚刚将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

svmlkihl

svmlkihl6#

我发现重写整个样式非常有用。

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>
zphenhs4

zphenhs47#

使用风格和定制根据您的要求

</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>
gzszwxb4

gzszwxb48#

试试看:

yourProgressBarName.Foreground = new SolidColorBrush(Colors.Red);

相关问题