Eamonn McEvoy Eamonn McEvoy - 1 year ago 161
C# Question

Binding the Loaded event?

I am trying to display a login window once my MainWindow loads while sticking to the MVVM pattern. So I am trying to Bind my main windows Loaded event to an event in my viewmodel.
Here is what I have tried:


<Window x:Class="ScrumManagementClient.MainWindow"
Title="MainWindow" Height="350" Width="525"
Loaded="{Binding ShowLogInWindow}">



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScrumManagementClient.ViewModel
class MainWindowViewModel : ViewModelBase
public void ShowLogInWindow(object sender, EventArgs e)
int i = 0;

The error message I am getting is "Loaded="{Binding ShowLogInWindow}" is not valid. '{Binding ShowLogInWindow}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid."

Answer Source

You're going to have to use the System.Windows.Interactivity dll.

Then add the namespace in your XAML:


Then you can do stuff like:

    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding MyICommandThatShouldHandleLoaded}" />

Please note that you will have to use an ICommand (or DelegateCommand is you use Prism, or RelayCommand if you use MVVMLight), and the DataContext of your Window must hold that ICommand.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download