你可以使用一个行为!
public class ProgressBarSmoother
{
public static double GetSmoothValue(DependencyObject obj)
{
return (double)obj.GetValue(SmoothValueProperty);
}
public static void SetSmoothValue(DependencyObject obj, double value)
{
obj.SetValue(SmoothValueProperty, value);
}
public static readonly DependencyProperty SmoothValueProperty =
DependencyProperty.RegisterAttached("SmoothValue", typeof(double), typeof(ProgressBarSmoother), new PropertyMetadata(0.0, changing));
private static void changing(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var anim = new DoubleAnimation((double)e.OldValue, (double)e.NewValue, new TimeSpan(0,0,0,0,250));
(d as ProgressBar).BeginAnimation(ProgressBar.ValueProperty, anim, HandoffBehavior.Compose);
}
}
您的 XAML 将如下所示:
<ProgressBar local:ProgressBarSmoother.SmoothValue="{Binding Progress}">
每当Progress
您在 xaml 中绑定的属性发生更改时,ProgressBarSmoother 行为中的代码就会运行,并使用适当的值To
和From
!