我真的没有找到与此主题相关的有用信息。
假设我将自定义ProgressBar定义为UserControl:
<ProgressBar x:Class="Views.Controls.ProgressBar"
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"
xmlns:local="clr-namespace:Views.Controls"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="300"
Value="50">
</ProgressBar>
我想让进度条动画流畅。我找到了一些解决方案,在控件本身中使用后台代码。从MVVM开始,这绝对不是如何处理这个问题。 主要目标是直接在xaml中处理它,触发Value属性值更改,然后启动Animation。但正如我已经提到的,我找不到任何有用的资源。
按照this链接的说明操作后,条形图不会填满。我的代码:
用户控件:
<UserControl x:Class="Views.Controls.ProgressBar"
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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:KBSLauncher.Views.Controls"
mc:Ignorable="d"
d:DesignWidth="200">
<Controls:MetroProgressBar x:Name="progressBar"/>
</UserControl>
背景代码:
public partial class ProgressBar: UserControl
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(ProgressBar), new PropertyMetadata(0d, ValueChanged));
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(ProgressBar));
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(ProgressBar));
public static readonly DependencyProperty IsIndeterminateProperty = DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(ProgressBar));
[Positive]
public double Value
{
get
{
return (double) GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
[StrictlyPositive]
public double Maximum
{
get
{
return progressBar.Maximum;
}
set
{
progressBar.Maximum = value;
}
}
[Positive]
public double Minimum
{
get
{
return progressBar.Minimum;
}
set
{
progressBar.Minimum = value;
}
}
public bool IsIndeterminate
{
get
{
return progressBar.IsIndeterminate;
}
set
{
progressBar.IsIndeterminate = value;
}
}
public ProgressBar()
{
InitializeComponent();
}
private static void ValueChanged (DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
ProgressBar control = (ProgressBar)sender;
control.ValueChanged();
}
private void ValueChanged()
{
DoubleAnimation animation = new DoubleAnimation();
animation.To = Value;
animation.Duration = new Duration(TimeSpan.FromMilliseconds(Value * 2));
Storyboard.SetTarget(animation, progressBar);
Storyboard.SetTargetProperty(animation, new PropertyPath(ValueProperty));
Storyboard sb = new Storyboard();
sb.Children.Add(animation);
sb.Begin();
}
}