将命令绑定到视图的Loaded事件

35

我试图在视图加载完成时运行一个方法。我尝试将命令绑定到视图中的Loaded事件,但它无法运行。抛出的内部异常是:

'System.Windows.Data.Binding'上的提供值引发了异常。'行号为'14',行位置为'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}">

我该如何绑定到视图的Loaded事件,以便在视图加载完成后运行一些操作?

2个回答

57

如果您想将命令绑定到Loaded事件,您应该使用"System.Windows.Interactivity"程序集。

<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位于Microsoft Expression Blend软件开发工具包(SDK)中(下载链接),同时也可作为NuGet软件包获取。


1
虽然我本来想采用CallMethodAction的方法,但你比我快了几秒钟。+1 - Chris W.
3
谢谢您的回答。顺便说一下,无需下载.dll文件,因为它已经位于.NET框架程序集 (...\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll) 中。 - Eido95
3
看起来 "System.Windows.Interactivity" 已经开源了。请查看 https://github.com/microsoft/XamlBehaviorsWpf。 - Tombatron
1
此 Nuget 包已被废弃,因为它是遗留的且不再维护。下载链接也已失效。 - dp2050

3
  1. 安装nuget包Microsoft.Xaml.Behaviors.Wpf (System.Windows.Interactivity已经弃用)

  2. 向窗口/用户控件添加xmlns。 xmlns:b="http://schemas.microsoft.com/xaml/behaviors"

  3. 将触发器添加到主窗口

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接