WPF自定义命令
WPF自定义命令,以下实例为实现删除功能的命令。
1、创建DeleteCommand.cs类,并实现ICommand接口
命令实现了ICommand接口并继承了CanExecuteChanged事件、CanExecute方法和Execute方法。
/// <summary>
/// 删除命令
/// </summary>
public class DeleteCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
int Id = (int)parameter;
if (Id <= 0)
{
return;
}
//执行删除方法
}
}
使用
<Window.Resources>
<!--引用资源-->
<command:DeleteCommand x:Key="DeleteCommand"/>
</Window.Resources>
<Button Content="删除" Command="{StaticResource DeleteCommand}" CommandParameter="{Binding Id}"></Button>
还没有评论,来说两句吧...