Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire a Command when a window is loaded in wpf

Is it possible to fire a command to notify the window is loaded. Also, I'm not using any MVVM frameworks (Frameworks in the sense, Caliburn, Onxy, MVVM Toolkit etc.,)

like image 293
Prince Ashitaka Avatar asked Aug 04 '10 17:08

Prince Ashitaka


2 Answers

This is much easier to do now. Simply include the following namespace:

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

And leverage it like this:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding CommandInViewModel}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
like image 186
dasch88 Avatar answered Oct 16 '22 22:10

dasch88


    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
       ApplicationCommands.New.Execute(null, targetElement); 
       // or this.CommandBindings[0].Command.Execute(null); 
    }

and xaml

    Loaded="Window_Loaded"
like image 30
Lukasz Madon Avatar answered Oct 16 '22 22:10

Lukasz Madon