Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:36:53+00:00 2026-06-03T21:36:53+00:00

I have a very simple UserControl that starts a progess bar animation when it

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:36:56+00:00Added an answer on June 3, 2026 at 9:36 pm

    Instead of

    w.SizeToContent = SizeToContent.WidthAndHeight
    

    use that code, its messy i agree but it works

       w.SourceInitialized += (s, a) =>
                {
    
                    w.SizeToContent = SizeToContent.WidthAndHeight;
    
                    w.UpdateLayout();
                    w.Left = this.Left + (this.ActualWidth / 2.0 - w.ActualWidth / 2.0);
                    w.Top = this.Top + (this.ActualHeight / 2.0 - w.ActualHeight / 2.0);
                };
    

    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 SourceInitialized event is fired. I tried the same with your code and it seems like a similar problem, because this did the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have very simple problem. I need to create model, that represent element of
I have some very simple javascript code that looks like this: var newWindow =
I have a very simple bit of script that changes the status of an
I have a very simple binding problem with a DatePicker that is eluding me.
I have a very simple UserControl as shown below. I'm trying to get the
I have some very simple code that 'correctly' draws a short vertical black line
I have a very simple WPF user control that is mixed in with a
Very simple and stupid question. i have a page class public partial class ProtectWayItem
I have very simple query. I want to make sure that I don't have
I have this very simple control below. And on the page that i use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.