我有一个非常简单的 UserControl,它在加载时启动进度条动画:
<UserControl x:Class="WpfApplication2.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ProgressBar Width="200" Height="10" Maximum="{Binding Delay}" SmallChange="1">
<ProgressBar.Triggers>
<EventTrigger RoutedEvent="ProgressBar.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Value" To="{Binding Delay}" Duration="00:00:10" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ProgressBar.Triggers>
</ProgressBar>
</UserControl>
动画的To
值来自 UserControl 的 DataContext:
class ViewModel
{
public int Delay
{
get { return 10; }
}
}
UserControl 显示在一个窗口中,如下所示:
var w = new Window();
var vm = new ViewModel();
var c = new UserControl1 {DataContext = vm};
w.Content = c;
w.Owner = this;
w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
// to make the animation play, I have to remove the following line
w.SizeToContent = SizeToContent.WidthAndHeight;
w.ShowDialog();
只要我有线,动画就不会播放
w.SizeToContent = SizeToContent.WidthAndHeight;
在我的代码中。
有没有人对这种非常奇怪的行为有解释并能提出解决方案?
最终目标非常简单。我想要的只是在加载 UserControl/Window 后一段时间(由 viewmodel 指定)的进度条动画。最好是仅 XAML。