Bind command to Loaded event of view
Asked Answered
W

2

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?

Whispering answered 29/7, 2013 at 20:0 Comment(0)
B
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.

Blest answered 29/7, 2013 at 20:11 Comment(4)
+1 Beat me by seconds, though I was going to go the CallMethodAction action route.Washtub
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)Hartzog
Looks like "System.Windows.Interactivity" has been open sourced. Checkout github.com/microsoft/XamlBehaviorsWpf .Civilize
This Nuget package has been deprecated as it is legacy and no longer maintained. The download link is also broken.Crossbred
K
4
  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>
Kozlowski answered 5/8, 2022 at 17:50 Comment(1)
That's the way!!! Especially if you have an async method, using the Community ToolkitPomade

© 2022 - 2024 — McMap. All rights reserved.