I have a very simple UserControl that starts a progess bar animation when it is loaded:
<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>
The animation’s To value comes from the DataContext of UserControl:
class ViewModel
{
public int Delay
{
get { return 10; }
}
}
The UserControl is shown in a window, all put together like this:
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();
The animation is not playing as long as I have the line
w.SizeToContent = SizeToContent.WidthAndHeight;
in my code.
Does anyone have an explanation for this very weird behavior and can come up with a solution?
The end goal is pretty simple. All I want is a progress bar animating for a period of time (speicified by viewmodel) right after the UserControl/Window was loaded. Preferably XAML-only.
Instead of
use that code, its messy i agree but it works
i don’t have an explanation for that unfortunately. I only know that the way WPF works and the way how Windows are treated internally in windows itself, can conflict sometimes. I had the same problem with setting the left/top/Width/height of a window and then directly maximizing it. To solve it i just delayed it until the
SourceInitializedevent is fired. I tried the same with your code and it seems like a similar problem, because this did the trick.