这里主要实现的效果是窗体在关闭的时候,需要弹出消息框确认是否关闭,来拦截窗体的Closing事件,无需通过Loaded事件传递控件对象到ViewModel中。
1.创建WPF项目,需要引入System.Windows.Interactivity.dll
2.创建EventToCommand类并继承至TriggerAction<DependencyObject>类,在xaml引入并实现Command绑定,具体实现代码如下:
public class EventToCommand : TriggerAction<DependencyObject>
{
public bool PassEventArgsToCommand { get; set; }
#region Command
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand),
typeof(EventToCommand), new PropertyMetadata(null));
public ICommand Command
{
get
{
return (ICommand)GetValue(CommandProperty);
}
set
{
SetValue(CommandProperty, value);
}
}
#endregion
#region CommandParameter
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object),
typeof(EventToCommand), new PropertyMetadata(null));
public object CommandParameter
{
get
{