WPF笔记汇总之命令的使用

WPF命令的使用

接上一篇《WPF笔记汇总之数据绑定及依赖属性》,这篇主要总结WPF特有的关于命令的用法,包括系统命令使用及自定义命令的创建及使用。

1. 命令概述

在WPF中,允许在一个地方定义命令,并且在所有的用户接口控件之中调用这些命令,它们由ICommand接口组成,该接口仅定义一个事件和两个方法:Execute()和CanExecute()。第一个用于执行实际操作,而第二个用于确定操作当前是否可用。要执行命令的实际操作,您需要在命令和代码之间使用CommandBinding作为链接,CommandBinding通常在Window或UserControl上定义,并保存对它处理的Command的引用,以及用于处理Command的Execute()和CanExecute()事件的实际事件处理程序。

2. 使用命令

2.1 使用系统自带命令
<DockPanel>
	<WrapPanel DockPanel.Dock="Top" Margin="3">
		<Button Command="ApplicationCommands.Cut" CommandTarget="{Binding ElementName=txtEditor}" Width="60">_Cut</Button>
		<Button Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=txtEditor}" Width="60" Margin="3,0">_Paste</Button>
	</WrapPanel>
	<TextBox AcceptsReturn="True" Name="txtEditor" />
</DockPanel>
2.2 自定义系统命令
<Window.CommandBindings>
	<CommandBinding Command="ApplicationCommands.Cut" CanExecute="CutCommand_CanExecute" Executed="CutCommand_Executed" />
	<CommandBinding Command="ApplicationCommands.Paste" CanExecute="PasteCommand_CanExecute" Executed="PasteCommand_Executed" />
</Window.CommandBindings>

<DockPanel>
	<WrapPanel DockPanel.Dock="Top" Margin="3">
		<Button Command="ApplicationCommands.Cut" Width="60">_Cut</Button>
		<Button Command="ApplicationCommands.Paste" Width="60" Margin="3,0">_Paste</Button>
	</WrapPanel>
	<TextBox AcceptsReturn="True" Name="txtEditor" />
</DockPanel>

private void CutCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
	e.CanExecute = (txtEditor != null) && (txtEditor.SelectionLength > 0);
}
private void CutCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
	txtEditor.Cut();
}
private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
	e.CanExecute = Clipboard.ContainsText();
}
private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
	txtEditor.Paste();
}

3. 自定义命令

3.1 使用ICommand创建
  • 第一步:创建命令
class CustomCommand : ICommand
{
    public event EventHandler CanExecuteChanged;
    public void Execute(object param)  
    {
        ExecuteAction?.Invoke(param);
    }
    public bool CanExecute(object param)  
    {
        if (CanExecuteAction != null)
            return CanExecuteAction(param);
        return false;
    }
    public Action<object> ExecuteAction { get; set; }
    public Func<object, bool> CanExecuteAction { get; set; }
}

  • 第二步:ViewModel中使用命令

public CustomCommand MyCommand { get; set; }

public void DoSomething(object param){
}
public bool CanDoSomething(object param){
    return true;  
}
public MyViewModel(){
    MyCommand = new CustomCommand();
    MyCommand.ExecuteAction = new Action<object>(this.DoSomething);
    MyCommand.CanExecuteAction = new Func<object, bool>(this.CanDoSomething);
}
  • 第二步:View中调用
<Window xmlns:vm="clr-namespace:MyApp.ViewModel" ... />
    <Grid>
        <Grid.DataContext>
            <vm:MyViewModel> 
        </Grid.DataContext>
        <Button Content="Click here" Command="{Binding MyCommand}" />
    </Grid>
</Window>

3.2 使用RoutedUICommand创建
  • 第一步:创建一个命令
public static class CustomCommands
{
    public static readonly RoutedUICommand ExitCommand = new RoutedUICommand(
        "quit app", 
        "ExitCommand",
        typeof(CustomCommands),
        new InputGestureCollection() { 
            new KeyGesture(Key.W, ModifierKeys.Control)  
        });
}

  • 第二步:完成两个事件
public void ExitCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
    e.Handled = true;
}
public void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true; 
    e.Handled = true;
}

  • 第三步:调用
<Window.CommandBindings>
    <CommandBinding Command="local:CustomCommands.ExitCommand" 
        CanExecute="ExitCommand_CanExecute" 
        Executed="ExitCommand_Execute"/>
</Window.CommandBindings>

<Button Content="Exit" Command="local:CustomCommands.ExitCommand"/>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值