WPF/Windows 7:禁用默认进度栏辉光动画

Bra*_*don 8 wpf animation aero .net-3.5 progress-bar

快速WPF问题 - 在WPF上的Win 7(我假设Vista),默认进度条做了一个漂亮的小发光"飞快移动"-y动画.

我在一个屏幕上显示了大约48个内容的进度,并且让所有这些东西都在你身上who - - - 你能禁用这些动画而不影响应用程序中的其他默认动画吗?

小智 10

罗伯特的答案很强大.这是一个hack(因为它依赖于执行发光的元素的内部名称,这是一个实现细节,可能在后续版本中更改):

void SetGlowVisibility(ProgressBar progressBar, Visibility visibility) {
    var glow = progressBar.Template.FindName("PART_GlowRect", progressBar) as FrameworkElement;
    if (glow != null) glow.Visibility = visibility;
}
Run Code Online (Sandbox Code Playgroud)

如果进度条的实现方式发生变化,这个黑客可能会停止工作.

另一方面,完全取代XAML和样式的解决方案可以锁定并修复颜色,边框等,并禁用将来可能添加到更新版本的ProgressBar的行为...


boh*_*nko 6

简单的非动画进度条可以写成带有两个填充矩形的网格:左边的一个用绿色填充,右边用灰色填充.

Grid将有两个列定义.

改变两者的宽度会影响进度的变化.


Rob*_*ser 4

我同意马修的评论,但无论如何,您的答案是应用没有动画的自定义样式。这是原始样式(通过反射器),您可以删除/调整/任何内容:

\n\n
<Style x:Key="{x:Type ProgressBar}" TargetType="{x:Type ProgressBar}">\n    <Style.Triggers>\n        <Trigger Property="Orientation" Value="Vertical">\n            <Setter Property="Template">\n                <Setter.Value>\n                    <ControlTemplate TargetType="{x:Type ProgressBar}">\n                        <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" SnapsToDevicePixels="true">\n                            <Border BorderThickness="1,1,1,0" BorderBrush="#BEBEBE" CornerRadius="2">\n                                <Border BorderThickness="1" BorderBrush="#EFEFEF" CornerRadius="1">\n                                    <DockPanel Name="PART_Track" Margin="0,0,0,1" LastChildFill="false">\n                                        <Decorator Name="PART_Indicator" Dock="Bottom">\n                                            <Rectangle LayoutTransform="{RotateTransform Angle=-90}">\n                                                <Rectangle.Fill>\n                                                    <MultiBinding Converter="{theme:ProgressBarBrushConverter}">\n                                                        <Binding Path="Foreground" RelativeSource="{RelativeSource TemplatedParent}" />\n                                                        <Binding Path="IsIndeterminate" RelativeSource="{RelativeSource TemplatedParent}" />\n                                                        <Binding Path="ActualHeight" ElementName="PART_Indicator" />\n                                                        <Binding Path="ActualWidth" ElementName="PART_Indicator" />\n                                                        <Binding Path="ActualHeight" ElementName="PART_Track" />\n                                                    </MultiBinding>\n                                                </Rectangle.Fill>\n                                            </Rectangle>\n                                        </Decorator>\n                                    </DockPanel>\n                                </Border>\n                            </Border>\n                        </Border>\n                    </ControlTemplate>\n                </Setter.Value>\n            </Setter>\n        </Trigger>\n    </Style.Triggers>\n    <Setter Property="Foreground" Value="{StaticResource [0] \xc3\x91}" />\n    <Setter Property="Background" Value="{DynamicResource {x:Static WindowBrush}}" />\n    <Setter Property="BorderBrush" Value="#686868" />\n    <Setter Property="BorderThickness" Value="1" />\n    <Setter Property="Template">\n        <Setter.Value>\n            <ControlTemplate TargetType="{x:Type ProgressBar}">\n                <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" SnapsToDevicePixels="true">\n                    <Border BorderThickness="1,1,1,0" BorderBrush="#BEBEBE" CornerRadius="2">\n                        <Border BorderThickness="1" BorderBrush="#EFEFEF" CornerRadius="1">\n                            <DockPanel Name="PART_Track" Margin="1,0,0,1" LastChildFill="false">\n                                <Rectangle Name="PART_Indicator">\n                                    <Rectangle.Fill>\n                                        <MultiBinding Converter="{theme:ProgressBarBrushConverter}">\n                                            <Binding Path="Foreground" RelativeSource="{RelativeSource TemplatedParent}" />\n                                            <Binding Path="IsIndeterminate" RelativeSource="{RelativeSource TemplatedParent}" />\n                                            <Binding Path="ActualWidth" ElementName="PART_Indicator" />\n                                            <Binding Path="ActualHeight" ElementName="PART_Indicator" />\n                                            <Binding Path="ActualWidth" ElementName="PART_Track" />\n                                        </MultiBinding>\n                                    </Rectangle.Fill>\n                                </Rectangle>\n                            </DockPanel>\n                        </Border>\n                    </Border>\n                </Border>\n            </ControlTemplate>\n        </Setter.Value>\n    </Setter>\n</Style>\n
Run Code Online (Sandbox Code Playgroud)\n\n

和转换器类:

\n\n
public class ProgressBarBrushConverter : IMultiValueConverter\n{\n// Methods\npublic object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)\n{\n    Type type = typeof(double);\n    if (((((values == null) || (values.Length != 5)) || ((values[0] == null) || (values[1] == null))) || (((values[2] == null) || (values[3] == null)) || ((values[4] == null) || !typeof(Brush).IsAssignableFrom(values[0].GetType())))) || ((!typeof(bool).IsAssignableFrom(values[1].GetType()) || !type.IsAssignableFrom(values[2].GetType())) || (!type.IsAssignableFrom(values[3].GetType()) || !type.IsAssignableFrom(values[4].GetType()))))\n    {\n        return null;\n    }\n    Brush brush = (Brush) values[0];\n    bool flag = (bool) values[1];\n    double d = (double) values[2];\n    double num2 = (double) values[3];\n    double num3 = (double) values[4];\n    if ((((d <= 0.0) || double.IsInfinity(d)) || (double.IsNaN(d) || (num2 <= 0.0))) || (double.IsInfinity(num2) || double.IsNaN(num2)))\n    {\n        return null;\n    }\n    DrawingBrush brush2 = new DrawingBrush();\n    brush2.Viewport = brush2.Viewbox = new Rect(0.0, 0.0, d, num2);\n    brush2.ViewportUnits = brush2.ViewboxUnits = BrushMappingMode.Absolute;\n    brush2.TileMode = TileMode.None;\n    brush2.Stretch = Stretch.None;\n    DrawingGroup group = new DrawingGroup();\n    DrawingContext context = group.Open();\n    double x = 0.0;\n    double width = 6.0;\n    double num6 = 2.0;\n    double num7 = width + num6;\n    if (flag)\n    {\n        int num8 = (int) Math.Ceiling((double) (d / num7));\n        double num9 = -num8 * num7;\n        double num10 = d * 0.3;\n        brush2.Viewport = brush2.Viewbox = new Rect(num9, 0.0, num10 - num9, num2);\n        TranslateTransform transform = new TranslateTransform();\n        double num11 = num8 * 100;\n        DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();\n        animation.Duration = new Duration(TimeSpan.FromMilliseconds(num11));\n        animation.RepeatBehavior = RepeatBehavior.Forever;\n        for (int i = 1; i <= num8; i++)\n        {\n            double num13 = i * num7;\n            animation.KeyFrames.Add(new DiscreteDoubleKeyFrame(num13, KeyTime.Uniform));\n        }\n        transform.BeginAnimation(TranslateTransform.XProperty, animation);\n        brush2.Transform = transform;\n        while ((x + width) < num10)\n        {\n            context.DrawRectangle(brush, null, new Rect(num9 + x, 0.0, width, num2));\n            x += num7;\n        }\n        d = num10;\n        x = 0.0;\n    }\n    while ((x + width) < d)\n    {\n        context.DrawRectangle(brush, null, new Rect(x, 0.0, width, num2));\n        x += num7;\n    }\n    double num14 = d - x;\n    if ((!flag && (num14 > 0.0)) && (Math.Abs((double) (d - num3)) < 1E-05))\n    {\n        context.DrawRectangle(brush, null, new Rect(x, 0.0, num14, num2));\n    }\n    context.Close();\n    brush2.Drawing = group;\n    return brush2;\n}\n\npublic object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)\n{\n    return null;\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n