WPF命令

理解命名

新特性:1、将事件委托到适当的命令 2、使控件的启用状态和相应命令的状态保持同步

命令:表示应用程序任务,并且跟踪任务是否能够被执行,然而,命令实际上不包含执行应用程序任务的代码。

命令绑定:每个命令绑定针对用户界面的具体区域,将命令连接到相关的应用程序逻辑。

命令源:命令源触发命令。

命令目标:执行命令的元素。

ICommand接口

WPF命令模型核心是System.Windows.Input.ICommand接口

public interface ICommand
{
    event EventHandler CanExecuteChanged;
    
    bool CanExecute(object parameter);
    void Execute(object parameter);
}

Excute()方法包含应用程序任务逻辑。CanExecute()方法返回命令的状态,当命令可用时命令源启用自身,不可用时禁用自身。具体实现可以参考MvvmLight源码。CommandManager.InvalidateRequerySuggested(); 刷新命令状态。

RoutedCommandRoutedUICommand

RoutedCommand实现ICommandtarget是处理事件的元素。

public bool CanExecute(object parameter, IInputElement target)
{
	throw null;
}
public void Execute(object parameter, IInputElement target)
{
}

RoutedUICommand:RoutedCommand,应用程序处理的大部分命令是RoutedUICommand

命令库

WPF提供了一个基本命令库。ApplicationCommands、NavigationCommands、EditingCommands、ComponentCommands、MediaCommands通过以上静态类的静态属性提供。

ApplicationCommand.Open示例:

<!--XAML代码实现--> 
<!--<Window.InputBindings>
        <KeyBinding
            Key="O" Command="ApplicationCommands.Open"
            Modifiers="Ctrl" />
    </Window.InputBindings>-->
    <Window.CommandBindings>
        <CommandBinding
            CanExecute="CommandBinding_CanExecute"
            Command="ApplicationCommands.Open"
            Executed="CommandBinding_Executed" />
    </Window.CommandBindings>

  <Grid>
        <StackPanel>
            <Button
                Width="100"
                Height="20"
                Command="ApplicationCommands.Open"
                Content="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Command.Text}" />
        </StackPanel>
    </Grid>

代码实现为:

//C#代码实现
//KeyBinding keyBinding = new KeyBinding(ApplicationCommands.Open, Key.O, ModifierKeys.Control);
//this.InputBindings.Add(keyBinding);
CommandBinding commandBinding = new CommandBinding(ApplicationCommands.Open, CommandBinding_Executed, CommandBinding_CanExecute);
this.CommandBindings.Add(commandBinding);

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
	MessageBox.Show(e.Source.ToString());
}  
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
	e.CanExecute = true;
}

通过观察弹出窗口信息,可以知道通过点击按钮弹出和快捷键弹出的命令源是不同的,分别button和window,命令库中的一些命令可以被自动映射到指定的组合键,如ApplicationCommands.Open被映射到Ctrl+O组合键。

WPF命令库命令可以简写为

<Button Command="Open"/>

命令源

实现ICommandSource接口的对象

public interface ICommandSource
{
    //指向链接的命令
	ICommand Command {get;}
    //命令参数
	object CommandParameter {get; }
    //执行命令的元素
	IInputElement CommandTarget {get;}
}

内置命令的控件

例如,TextBox类处理CutCopyPaste命令以及来自EditingCommand的一些命令。ToolBar和Menu控件可以将它的子元素的CommanTarget属性自动设置为具有焦点的空间。其他容器中则要手动设置CommandTarget属性,或者使用FocusManager.IsFocusScope附加属性。

 <Button Command="Copy" CommandTarget="{Binding ElementName=textBox}" />

<!--FocusManager.IsFocusScope 在父元素的焦点范围中查找-->
<StackPanel FocusManager.IsFocusScope="True">
   <Button Command="Cut" />
    <Button Command="Copy"  />
    <Button Command="Paste"  />
</StackPanel>

内置属性控件可以通过IsUndoEnable属性来移除内置命令,或者通过CommandBindingKeyBindind来禁用命令。

posted @ 2022-04-23 20:57  蓝白永恒  阅读(412)  评论(0编辑  收藏  举报