Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window_Load event in MVVM

I need to write some functions to be executed during the window_load() in WPF-MVVM. Every button will have their own command to be executed. Whereas is there any command available for window_load() event in MVVM Model ?

like image 975
ognale88 Avatar asked Sep 03 '13 13:09

ognale88


People also ask

What is WPF in MVVM?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.

What is MVVM command?

Commands are an implementation of the ICommand interface that is part of the . NET Framework. This interface is used a lot in MVVM applications, but it is useful not only in XAML-based apps.


1 Answers

You will have to use interactions to do that i.e to invoke command on event.

<Window
    xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
>
    <intr:Interaction.Triggers>
        <intr:EventTrigger EventName="Loaded">
            <intr:InvokeCommandAction Command="{Binding WindowLoaded}"/>
        </intr:EventTrigger>
    </intr:Interaction.Triggers>
    <!-- the rest of your XAML here -->
</Window>

Window.Interactivity namespace has EventTrigger and InvokeCommandAction.

Don't forget that the WindowLoaded is a property.

public ICommand WindowLoaded { get; set; }

You later have to create new RelayCommand/RoutedUICommand to actually receive the callback.

Thanks

like image 102
Nitin Avatar answered Oct 19 '22 01:10

Nitin