本文经原作者授权以原创方式二次分享,欢迎转载、分享。
原文作者:蓝白永恒
原文地址:https://www.cnblogs.com/wzmcnblogs/p/16183586.html
理解命名
-
新特性:
1)将事件委托到适当的命令;
2)使控件的启用状态和相应命令的状态保持同步; -
命令:表示应用程序任务,并且跟踪任务是否能够被执行,然而,命令实际上不包含执行应用程序任务的代码。
-
命令绑定:每个命令绑定针对用户界面的具体区域,将命令连接到相关的应用程序逻辑。
-
命令源:命令源触发命令。
-
命令目标:执行命令的元素。
ICommand 接口
1)WPF命令模型核心是System.Windows.Input.ICommand
接口;
public interface ICommand
{
event EventHandler CanExecuteChanged;
bool CanExecute(object parameter);
void Execute(object parameter);
}
-
Excute()
方法包含应用程序任务逻辑。 -
CanExecute()
方法返回命令的状态,当命令可用时命令源启用自身,不可用时禁用自身。具体实现可以参考MvvmLight
源码。 -
CommandManager.InvalidateRequerySuggested();
刷新命令状态。
RoutedCommand 和 RoutedUICommand
1) RoutedCommand
实现 ICommand
,target
是处理事件的元素。
public bool CanExecute(object parameter, IInputElement target)
{
throw null;
}
public void Execute(object parameter, IInputElement target)
{
}
2) RoutedUICommand:RoutedCommand
,应用程序处理的大部分命令是RoutedUICommand
。
命令库
1) 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>
cs
代码实现为:
//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;
}
2)通过观察弹出窗口信息,可以知道通过点击按钮弹出和快捷键弹出的命令源是不同的,分别button
和window
,命令库中的一些命令可以被自动映射到指定的组合键,如ApplicationCommands.Open
被映射到Ctrl+O
组合键。
3)WPF
命令库命令可以简写为
<Button Command="Open"/>
命令源
1)实现ICommandSource
接口的对象;
public interface ICommandSource
{
//指向链接的命令
ICommand Command {get;}
//命令参数
object CommandParameter {get; }
//执行命令的元素
IInputElement CommandTarget {get;}
}
内置命令的控件
1)例如TextBox
类处理Cut、Copy、Paste
命令以及来自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
属性来移除内置命令,或者通过CommandBinding
和KeyBindind
来禁用命令。
原文始发于微信公众号(WPF开发者):WPF 命令
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/55111.html