MVVM Light Toolkit包含一個名爲EventToCommand的行爲,該行爲爲您提供了將命令綁定到事件的簡單方法。
下面的XAML片段顯示獲得一個命令調用如何「CloseCommand」執行時,窗口的關閉事件引發的一個例子:
<Window x:Class="EventToCommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
Title="MainWindow" Height="300" Width="500">
<!-- Make sure to put this tag directly inside the Window,
and not inside a child element, since it is the Windows that has the Closed event -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closed">
<cmd:EventToCommand Command="{Binding CloseCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<!-- Windows contents -->
</Window>
要訪問的EventToCommand行爲,你需要下載MVVM光工具包從project downloads page,然後引用下列DLL:
- GalaSoft.MvvmLight.dll
- GalaSoft.MvvmLight.Extras.dll
- System.Windows.Interactivity.dll
這就是所有需要的。
關於如何開始使用工具包的進一步說明可以在here找到。
http://www.codewrecks.com/blog/index.php/2011/05/04/event-to-command-in-wpf-mvvm-application/ – michael