wpf

[WPF] Load event

We don’t want to add Load event in Code behind in order to keep MVVM standard. One place we can store it is in ViewModel.

Step 1: Add following in Reference

System.Window.Interactivity
Microsoft.Expression.Interactions

Step 2: Reference in header of View we want to add Load event

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

Step 3: Add following code in XAML

<i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <ei:CallMethodAction TargetObject="{Binding}" MethodName="Load"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

Step 4: In ViewModel, add Load event:

public void Load()
        {
            //Do something
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s