WPF CommandBinding和InputBindings

button的Command在绑定一个命令时,被绑定的命令是实现了ICommand接口的类对象:

mvvm模式下,与业务逻辑有关的Command绑定ViewModel或Model中的命令,这个命令必须是继承自ICommand的类的实例

与业务逻辑无关的命令,只和界面相关的 Command绑定RoutedCommand或RoutedUICommand,这2个也是继承自ICommand

其中还有继承自RoutedUICommand的一些系统内置的命令。

  ApplicationCommands:提供一组标准的与应用程序相关的命令 

  ComponentCommands:提供一组标准的与组件相关的命令 

  NavigationCommands:提供一组标准的与导航相关的命令 

  MediaCommands:提供一组标准的与媒体相关的命令 

  EditingCommands:提供一组标准的与编辑相关的命令 

1.CommandBinding

ApplicationCommands.New

 <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.New"
                        Executed="CommandBinding_Executed_1"
                        CanExecute="CommandBinding_CanExecute_1"/>
        </Window.CommandBindings>
    <Grid>
        <StackPanel>
            <Button Height="30" Width="30" Command="ApplicationCommands.New"
                    Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"
                    CommandManager.Executed="CommandBinding_Executed_1"
                    CommandManager.CanExecute="CommandBinding_CanExecute_1" />

            <TextBox Name="textbox"/>
        </StackPanel>
    </Grid>
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
        {
           //dosomething
        }

        private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
        {
            if(this.textbox != null)
            {
                if(this.textbox.Text=="")
                {
                    e.CanExecute = true;
                }
            }   
        }

命令的特点就是可以通过 CommandManager.CanExecute去添加一些逻辑,通过e.Canexecute的值来决定此操作是否可用。

textbox的text为空,则“新建”按钮不可用

textbox的text不为空,则“新建”按钮可用

 这些判断都是与业务逻辑无关的,所以不用写在ViewModel中。

第2个例子:不用写execute和惨execute的内置命令

2个textbox和2个button按钮,分别绑定ApplicationCommands.Copy和ApplicationCommands.Paste

是否可复制和是否可粘贴的逻辑不需要我们写,对于复制来说,只要ApplicationCommands.Copy的Target有选中,则自动变更按钮状态

是否可粘贴,只要剪贴板有东西可以粘贴到ApplicationCommands.Paste的Target的目标中,则自动变更按钮状态

可复制时,“复制”按钮自动可用

当点击复制按钮后,“粘贴按钮自动可用”

 <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Copy"/>
        <CommandBinding Command="ApplicationCommands.New"
                        Executed="CommandBinding_Executed_1"
                        CanExecute="CommandBinding_CanExecute_1"/>
    </Window.CommandBindings>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <StackPanel >
            <TextBox Name="tb1"/>
            <Button Command="ApplicationCommands.Copy"
                    Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"
                    CommandTarget="{Binding ElementName=tb1}"/>
        </StackPanel>
        <StackPanel Grid.Column="1">
            <TextBox Name="tb2"/>
            <Button Command="ApplicationCommands.Paste"
                    Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"
                    CommandTarget="{Binding ElementName=tb2}"/>
        </StackPanel>
    </Grid>

 一个命令包含4部分:

(1)命令command:要执行的动作。

(2)命令源command source:发出命令的对象(继承自ICommandSource)。

(3)命令目标command target:执行命令的主体

(4)命令绑定command binding:被关联的对象

2.InputBindings

用来指定一些快捷键,通过快捷键去触发命令

 <Window.InputBindings>
        <KeyBinding Command="{Binding testcmd }" Modifiers="Ctrl" Key="D"/>
    </Window.InputBindings>
    <Grid>
        <Button Height="30" Width="30">
            <KeyBinding Command="{Binding testcmd}" Modifiers="Ctrl" Key="D"/>
        </Button>
    </Grid>

这里的command绑定的是ViewModel中的命令。

posted @ 2022-08-24 22:16  薛定谔的小灯泡  阅读(1022)  评论(0编辑  收藏  举报