WPF 中的进度条样式是老式的。Bars 中的增量。如何实现带有 vista 或 windows-7 阴影发光效果的进度条?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11967898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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?
提问by now he who must not be named.
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 ?
WPF 中的进度条样式是老式的。Bars 中的增量。如何实现带有 vista 或 windows-7 阴影发光效果的进度条?
Image http://quickshare.my3gb.com/download/2.JPG
图片 http://quickshare.my3gb.com/download/2.JPG
Even checked out the properties of the Progressbar. But, there is no properties related to glowy effect.
甚至检查了 Progressbar 的属性。但是,没有与发光效果相关的属性。
Also, is there any animations or something different from normal progress bar/
另外,是否有任何动画或与正常进度条不同的东西/
Thanks in Adv.
感谢 Adv。
Edit The Code
编辑代码
<ProgressBar Height="41" HorizontalAlignment="Left" Margin="372,215,0,0" Name="progressBar1" VerticalAlignment="Top" Width="150">
</ProgressBar>
回答by Charleh
Roll your own shouldn't be too hard.
自己动手应该不会太难。
Create a usercontrol which has the properties of a standard progress bar
创建一个具有标准进度条属性的用户控件
Value
Maximum
Minimum
You can create a derived property which calculates the size of the bar by using the a formula:
您可以创建一个派生属性,通过使用 a 公式计算条形的大小:
ProgressBarWidth = (Value / (Maximum + Minimum) * ControlWidth) - Padding
Which changes when the Value, Maximum or Minimum is updated
当值、最大值或最小值更新时会发生变化
Bind this to the Width of the 'bar' in your progress bar control template - this way when the Value property is updated, the progress bar will resize.
将此绑定到进度条控件模板中“条”的宽度 - 这样当 Value 属性更新时,进度条将调整大小。
How your bar looks is up to you, but I guess you just want a load of fancy fills/gradients/glow effects - you can add these in Blend
你的酒吧看起来如何取决于你,但我想你只是想要一堆花哨的填充/渐变/发光效果 - 你可以在 Blend 中添加这些
Disclaimer: Formulas may be incorrect!
免责声明:公式可能不正确!
In case you want to try and roll your own, here's one I just knocked up which seems to work ok
如果你想尝试自己动手,这里有一个我刚刚敲出来的,似乎工作正常
public partial class MyProgressBar : UserControl
{
public MyProgressBar()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MyProgressBar_Loaded);
}
void MyProgressBar_Loaded(object sender, RoutedEventArgs e)
{
Update();
}
private static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(MyProgressBar), new PropertyMetadata(100d, OnMaximumChanged));
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
private static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(MyProgressBar), new PropertyMetadata(0d, OnMinimumChanged));
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
private static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(MyProgressBar), new PropertyMetadata(50d, OnValueChanged));
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static readonly DependencyProperty ProgressBarWidthProperty = DependencyProperty.Register("ProgressBarWidth", typeof(double), typeof(MyProgressBar), null);
private double ProgressBarWidth
{
get { return (double)GetValue(ProgressBarWidthProperty); }
set { SetValue(ProgressBarWidthProperty, value); }
}
static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
(o as MyProgressBar).Update();
}
static void OnMinimumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
(o as MyProgressBar).Update();
}
static void OnMaximumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
(o as MyProgressBar).Update();
}
void Update()
{
// The -2 is for the borders - there are probably better ways of doing this since you
// may want your template to have variable bits like border width etc which you'd use
// TemplateBinding for
ProgressBarWidth = Math.Min((Value / (Maximum + Minimum) * this.ActualWidth) - 2, this.ActualWidth - 2);
}
}
The XAML
XAML
<UserControl x:Class="WpfApplication1.MyProgressBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" x:Name="uc">
<Grid>
<Border Background="White">
<Border BorderBrush="Gray" BorderThickness="1">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE5E5E5" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Grid Width="{Binding ProgressBarWidth, ElementName=uc}" HorizontalAlignment="Left">
<Grid.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFCBFFD0" Offset="0" />
<GradientStop Color="#FF62EF73" Offset="1" />
<GradientStop Color="#FFAEE56D" Offset="0.39" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Grid>
</Border>
</Border>
</Grid>
</UserControl>
The result:
结果:


Like I said, something like this is pretty easy but still consider redefining the template or using the original since it does support glowyness on the right OS
就像我说的,这样的事情很简单,但仍然考虑重新定义模板或使用原始模板,因为它确实支持正确操作系统上的发光
Here it is after I added a 'Percent' dependency property and bound to that in the control template:
这是在我添加了一个“百分比”依赖属性并绑定到控件模板中的属性之后:


Code for updating Percentwas
更新代码Percent是
Percentage = Math.Min((int)(Value / (Maximum + Minimum) * 100), 100);
Edit 2:
编辑2:
I messed with the fills and added a white inner border so it looks more shiny. The only thing missing is the shiny animation
我弄乱了填充物并添加了一个白色的内边框,使其看起来更闪亮。唯一缺少的是闪亮的动画
the top one is my control, the bottom one is the default WPF one
上面一个是我的控件,下面一个是默认的 WPF 一个
Bear in mind, all of this may be possible just by editing the progress bar control template
请记住,所有这一切都可以通过编辑进度条控件模板来实现


Here's the updated XAML:
这是更新的 XAML:
<UserControl x:Class="WpfApplication1.MyProgressBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" x:Name="uc">
<Grid>
<Border Background="White" BorderBrush="Gray" BorderThickness="1">
<Border BorderBrush="White" BorderThickness="1">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE5E5E5" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<Grid Width="{Binding ProgressBarWidth, ElementName=uc, FallbackValue=200}" HorizontalAlignment="Left">
<Grid.Background>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FF8BBA91" Offset="0" />
<GradientStop Color="#FF8BBA91" Offset="1" />
<GradientStop Color="#FF9ED76A" Offset="0.8" />
<GradientStop Color="#FF9ED76A" Offset="0.2" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
<Border>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#89E2E2E2" Offset="0" />
<GradientStop Color="#C1FFFFFF" Offset="0.5" />
<GradientStop Color="Transparent" Offset="0.52" />
</LinearGradientBrush>
</Border.Background>
</Border>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Percentage, ElementName=uc}"></TextBlock>
</Grid>
</Border>
</Border>
</Grid>
</UserControl>
回答by Colin Smith
You need to be using the Aero theme (i.e. Windows 7 Basic, or Windows 7) in your Personalization settings of Windows in order to get the smooth progress bar and animated glow look.
您需要在 Windows 的个性化设置中使用 Aero 主题(即 Windows 7 Basic 或 Windows 7)才能获得流畅的进度条和动画发光外观。
Vista and Windows 7 have the Aero theme. It didn't even exist when Windows XP was released. It was also removed in Windows 8 for a more standard look across the board. If you switch to Windows Classic then you get the old chunky bars look for the progress bar.
Vista 和 Windows 7 具有 Aero 主题。它甚至在 Windows XP 发布时都不存在。它也在 Windows 8 中被删除,以获得更标准的全面外观。如果您切换到 Windows Classic,那么您会看到旧的粗条寻找进度条。
WPF has a different set of Templates defined for Luna, Aero, Royale, Classic.....what it does is look at the theme your system is currently using, and then this makes it decide which set of WPF Templates to use.
WPF 为 Luna、Aero、Royale、Classic 定义了一组不同的模板......它的作用是查看您的系统当前使用的主题,然后决定使用哪组 WPF 模板。
So when you are on XP, it never gets the opportunity to use the Aero set of templates.
因此,当您使用 XP 时,它永远不会有机会使用 Aero 模板集。
If for some reason you want to have your progress bar themed with the Aero look even when Windows is not using the Aero theme then there are 2 main solutions.
如果出于某种原因,即使 Windows 不使用 Aero 主题,您也希望以 Aero 外观为主题的进度条,那么有两种主要解决方案。
The solutions:
解决方法:
You can force your WPF application to use the Aero-themed WPF templates for all of its controls:
您可以强制 WPF 应用程序对其所有控件使用 Aero 主题的 WPF 模板:
http://arbel.net/2006/11/03/forcing-wpf-to-use-a-specific-windows-theme/
http://www.kennethham.com/wp/2010/11/force-wpf-to-use-windows-7aero-theming-in-classic-mode/
http://arbel.net/2006/11/03/forcing-wpf-to-use-a-specific-windows-theme/
http://www.kennethham.com/wp/2010/11/force-wpf-to-use-windows-7aero-theming-in-classic-mode/
OR
或者
If you don't want all of your applications controls to be themed as Aero, then just take the style/template that defines the Aero look of the ProgressBar from the Aero theme resources and apply it to your ProgressBar.
如果您不希望所有应用程序控件都以 Aero 为主题,则只需从 Aero 主题资源中获取定义 ProgressBar 的 Aero 外观的样式/模板,并将其应用到您的 ProgressBar。
Hopefully either way will get you your glowing progress bar....note, I don't have XP to test if this is the case.
希望任何一种方式都能让您获得发光的进度条....注意,我没有 XP 来测试是否是这种情况。
回答by James M
You can mimic the animation using the following, which is a tweak for the accepted answer:
您可以使用以下内容模仿动画,这是对已接受答案的调整:
<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>
Preview:
预览:
回答by GameAlchemist
If you want to change the look of the Progress Bar, you'll have to re-define its (Control)Template.
ms explains this here :
http://msdn.microsoft.com/en-us/library/ms750638.aspx
When writing the Template, to get the glow, You can use the BitmapEffect,
with a OuterGlowBitmapEffect (!Software rendered), but Best is to use Effect (more up to date).
如果你想改变进度条的外观,你必须重新定义它的(控制)模板。
ms 在这里解释了这一点:
http: //msdn.microsoft.com/en-us/library/ms750638.aspx
在编写模板时,要获得发光效果,您可以使用 BitmapEffect 和 OuterGlowBitmapEffect(!软件渲染),但是最好是使用 Effect(更新)。
Maybe you don't have to redefine the Template if you use the Effect of the progress bar and it suits you.
如果您使用进度条的效果并且它适合您,也许您不必重新定义模板。
Notice that you have good free themes for your application on the net.
请注意,您在网上的应用程序有很好的免费主题。

