你会发现,当程序正在进行某项耗时的工作的时候常常会显示一个旋转的圆形图标。在WPF中你可以非常简单的用Image 控件实现这个功能。
下面的例子中,你有一个当Busy 属性设置为为ture的时候会进行旋转动画的Image 控件(通过按钮点击触发)。
<StackPanel>
<Image Source="Images\Spinny3.jpg" Height="40" Width="40" Stretch="None"
HorizontalAlignment="Center" VerticalAlignment="Center" Margin="15">
<Image.RenderTransform>
<RotateTransform CenterX="20" CenterY="20"/>
</Image.RenderTransform>
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Busy}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="rotatingStoryboard">
<Storyboard>
<DoubleAnimation
Storyboard.Target="{Binding TemplatedParent}"
Storyboard.TargetProperty="(Image.RenderTransform).(RotateTransform.Angle)"
From="0" To="360" Duration="0:0:1" RepeatBehavior="Forever"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="rotatingStoryboard"/>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Button Content="Start-Stop" HorizontalAlignment="Center" Margin="15" Padding="10,5" Click="Button_Click"/>
</StackPanel>
后台代码中,实现Busy 属性,并且实现了属性更改通知。
public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool busy = false;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public bool Busy
{
set
{
this.busy = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("Busy"));
}
get
{
return this.busy;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Busy = !this.Busy;
}
}
你点击按钮后,将会看到旋转的图标。

原文地址:https://wpf.2000things.com/2012/03/06/508-displaying-a-spinning-busy-indicator/