Execute command after view is loaded WPF MVVM

command, mvvm, triggers, wpf

Solution

That's because even though technically the view is loaded (i.e: all the components are ready in memory), your app is not idle yet, and thus the UI isn't refreshed yet.

Setting a command using interaction triggers on the `Loaded` event is already good, as there is no better event to attach to. Now to really wait until the UI is shown, do this in your `StartProgress()` (I'm assuming here that this is the name of the method that `StartProgressCommand` point to):

public void StartProgress()
{
    new DispatcherTimer(//It will not wait after the application is idle.
                       TimeSpan.Zero,
                       //It will wait until the application is idle
                       DispatcherPriority.ApplicationIdle, 
                       //It will call this when the app is idle
                       dispatcherTimer_Tick, 
                       //On the UI thread
                       Application.Current.Dispatcher); 
}

private static void dispatcherTimer_Tick(object sender, EventArgs e)
{
    //Now the UI is really shown, do your computations
}

Problem

I have a project based WPF and MVVM. My project is based on a wizard containing a content control which shows my views (User Controls) I want to execute a command after the view is loaded completely, I would like the user to see the view UI immediately after the command will be executed. I tried using : ``` <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <i:InvokeCommandAction Command="{Binding StartProgressCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> ``` But the command is executed before I see the view UI and it's not what I'm looking for. Does anyone have an idea how should I need to implement it?

Original source