35

I am trying to get a method to run when a view has finished loading. I have tried to bind a command to the Loaded event in the view but it fails to run. The inner exception that is thrown is

'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '14' and line position '14'

<UserControl x:Class="Components.Map.MapView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:map="clr-namespace:Components.Map"
             xmlns:controls="clr-namespace:Windows.Controls;assembly=Windows.Controls"
             xmlns:ValidationRules="clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls"
             xmlns:directGraphicsControl="clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl"
             xmlns:colorBar="clr-namespace:Components.Common.ColorBar;assembly=Components.Common"
             xmlns:RefinedRibbonControls="clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common"
             xmlns:UserControls="clr-namespace:Components.Common.UserControls;assembly=Components.Common"
             xmlns:map1="clr-namespace:Models.Map;assembly=Models.Map"
             xmlns:utilities="clr-namespace:Windows.Utilities;assembly=Windows.Utilities"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             Loaded="{Binding Path=MapControlViewModel.MapLoadedCommand}">

How am I able to bind to a view’s Loaded event so I can run something after the view has finished loading?

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
scott lafoy
  • 1,001
  • 1
  • 13
  • 30

2 Answers2

57

If you want to bind command to the Loaded event, you should use the "System.Windows.Interactivity" assembly.

<UserControl x:Class="Components.Map.MapView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:map="clr-namespace:Components.Map"
             xmlns:controls="clr-namespace:Windows.Controls;assembly=Windows.Controls"
             xmlns:ValidationRules="clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls"
             xmlns:directGraphicsControl="clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl"
             xmlns:colorBar="clr-namespace:Components.Common.ColorBar;assembly=Components.Common"
             xmlns:RefinedRibbonControls="clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common"
             xmlns:UserControls="clr-namespace:Components.Common.UserControls;assembly=Components.Common"
             xmlns:map1="clr-namespace:Models.Map;assembly=Models.Map"
             xmlns:utilities="clr-namespace:Windows.Utilities;assembly=Windows.Utilities"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">

             <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <i:InvokeCommandAction Command="{Binding LoadedCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

</UserControl>

System.Windows.Interactivity.dll is in Microsoft Expression Blend Software Development Kit (SDK) (download link) and also available as a NuGet package.

Jordoff
  • 66
  • 8
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • 1
    +1 Beat me by seconds, though I was going to go the `CallMethodAction` action route. – Chris W. Jul 29 '13 at 20:13
  • 3
    Thank you for your answer. By the way, there is no need to download the .dll because it is already located in .NET framework assemblies (`...\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll`) – Eido95 May 24 '16 at 20:07
  • 3
    Looks like "System.Windows.Interactivity" has been open sourced. Checkout https://github.com/microsoft/XamlBehaviorsWpf . – Tombatron Sep 28 '19 at 11:32
  • 1
    This Nuget package has been deprecated as it is legacy and no longer maintained. The download link is also broken. – dp2050 Jan 16 '22 at 02:34
3
  1. Install nuget package Microsoft.Xaml.Behaviors.Wpf (System.Windows.Interactivity is deprecated)

  2. Add xmlns to window/user control. xmlns:b="http://schemas.microsoft.com/xaml/behaviors"

  3. Add Trigger to main window

    <b:Interaction.Triggers>
        <b:EventTrigger EventName="Loaded">
            <b:InvokeCommandAction
                CommandParameter="{Binding}"
                Command="{Binding MyLoadedCommand}"/>
        </b:EventTrigger>
    </b:Interaction.Triggers>