将命令绑定到已加载的视图事件

sco*_*foy 30 wpf xaml commandbinding

我试图在视图加载完成后获得一个方法来运行.我试图将命令绑定到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}">
Run Code Online (Sandbox Code Playgroud)

我怎么能绑定到视图的Loaded事件,所以我可以在视图加载完后运行一些东西?

kma*_*zek 50

如果要将命令绑定到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>
Run Code Online (Sandbox Code Playgroud)

System.Windows.Interactivity.dll位于Microsoft Expression Blend软件开发工具包(SDK)(下载链接)中.

  • 谢谢您的回答.顺便说一句,没有必要下载.dll,因为它已经位于.NET框架程序集中(`...\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries\System.Windows.Interactivity .dll`) (3认同)
  • 看起来“System.Windows.Interactivity”已经开源了。查看 https://github.com/microsoft/XamlBehaviorsWpf 。 (2认同)