wpf 将命令绑定到视图的加载事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17933102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind command to Loaded event of view
提问by scott lafoy
I am trying to get a method to run when a view has finished loading. I have tried to bind a command to the Loadedevent in the view but it fails to run. The inner exception that is thrown is
我正在尝试获取一种在视图加载完成后运行的方法。我试图将命令绑定到Loaded视图中的事件,但它无法运行。抛出的内部异常是
'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '14' and line position '14'
'在'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}">
How am I able to bind to a view’s Loadedevent so I can run something after the view has finished loading?
我如何能够绑定到视图的Loaded事件,以便在视图加载完成后运行某些内容?
回答by kmatyaszek
If you want to bind command to the Loadedevent, you should use the "System.Windows.Interactivity" assembly.
如果要将命令绑定到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 is in Microsoft Expression Blend Software Development Kit (SDK) (download link) and also available as a NuGet package.
System.Windows.Interactivity.dll 位于 Microsoft Expression Blend 软件开发工具包 (SDK)(下载链接)中,也可作为NuGet 包使用。

