需求:MVVM模式,在MainWindow的ViewModel接收到UserCOntrolB的Command事件
Command .cs
using System;
using System.Windows.Input;
namespace WpfApplication
{
public class Command : ICommand
{
Action<object> executeMethod;
Func<object, bool> canExecuteMethod;
public Command(Action<object> execute):this(execute,null)
{
}
public Command(Action<object> execute, Func<object, bool> canExecute)
{
executeMethod = execute;
canExecuteMethod = canExecute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return canExecuteMethod(parameter);
}
public void Execute(object parameter)
{
executeMethod(parameter);
}
}
}
UserControlB.xaml
<UserControl x:Class="WpfApplication.UserControlB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.micr