问题
The progress bars in Windows Forms applications have the standard "shine" animation, but when I try to add a progress bar in WPF I don't get such a thing by default. How do we get this back with WPF in Windows 8?
Windows Forms

WPF

回答1:
It's a rather bizzare fix, but you need to enable Windows Forms styles in your application to use the "gloss." Here's how I did it.
- Add a reference in your project to
System.Windows.Forms
- Go to the Project settings page and click
View Application Events
- Add a handler to your
Application.Startup
using the following code (in VB.NET, C# similar) (also, if you need to include the arguments, do so)
Class Application
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.
Private Sub Application_Startup() Handles Me.Startup
System.Windows.Forms.Application.EnableVisualStyles()
End Sub
End Class
It seems weird that you would have to call this in order to get the WPF progressbar to work, but the code works for me.
回答2:
If you want ultimate control over the look (namely the color of the indicator), I have tweaked a control I found on another SO post, which gives near-identical display.
Tweak:
<Grid Background="LightGray">
<Grid Width="{Binding ProgressBarWidth, ElementName=uc}" HorizontalAlignment="Left">
<Grid.Triggers>
<EventTrigger RoutedEvent="Rectangle.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[1].(GradientStop.Offset)" From="-1" To="0" Duration="0:0:1.5" RepeatBehavior="Forever"/>
<DoubleAnimation Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[2].(GradientStop.Offset)" From="0" To="1" Duration="0:0:1.5" RepeatBehavior="Forever"/>
<DoubleAnimation Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[3].(GradientStop.Offset)" From="1" To="2" Duration="0:0:1.5" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid.Background>
<LinearGradientBrush>
<GradientStop Color="#FF218ED6" Offset="0.0" />
<GradientStop Color="#FF4BA5E0" Offset="0.4" />
<GradientStop Color="#FF8ECCF5" Offset="0.5" />
<GradientStop Color="#FF4BA5E0" Offset="0.6" />
<GradientStop Color="#FF218ED6" Offset="1.0" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Grid>
Full Implementation:
Progress bar style in WPF is old fashioned. Increments in Bars. How to implement a progress bar with vista or windows-7 shady glow effect?
Preview:
回答3:
Milliron X answer didn't help me so I had to use Windows Forms ProgressBar
instead of reinventing the wheel using custom control.
To achieve this:
- Add reference to
WindowsFormsIntegration
andSystem.Windows.Forms
assemblies. Add following namespace to
Window
elementxmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Add
ProgressBar
toWindowsFormsHost
control. For example<WindowsFormsHost Margin="0,10,0,0"> <wf:ProgressBar x:Name="DownloadProgressBar" Width="500" Height="50" /> </WindowsFormsHost>
For our
ProgressBar
not to look "old-style" you need to add following line to your entry point (e.g.Main
method orApplication.OnStartUp
method):System.Windows.Forms.Application.EnableVisualStyles();
来源:https://stackoverflow.com/questions/17672058/windows-8-wpf-progress-bars-dont-have-glow-animation