WPF:使用CommandManager.RegisterClassCommandBinding和RegisterClassInputBinding方法
2013-02-25 22:54
357 查看
这两个方法可以在自定义控件中建立内置WPF命令,比如WPF中TextBox内置复制,剪切,粘贴命令。其中RegisterClassInputBinding将一个命令和输入笔势(Input Gesture)联系在一起。这样响应输入笔势发生后,命令逻辑会运行。而RegisterClassCommandBinding则将命令和具体命令执行逻辑和命令判断逻辑联系起来。这样一个命令就有具体逻辑执行代码了。
下面自定义一个控件,继承与Button类型,使得按钮中的文字支持剪切和粘贴。在自定义类型的静态构造函数中用RegisterClassCommandBinding把剪切和粘贴的命令(对应ApplicationCommands类型的Cut和Paste属性)执行实现。用RegisterClassInputBinding则定义命令执行的快捷键。
代码:
class
MyButton :
Button
{
static MyButton()
{
//使用CommandManager.RegisterClassCommandBinding和RegisterClassInputBinding方法
//注册我们需要的剪切和粘贴命令。
CommandManager.RegisterClassCommandBinding(typeof(MyButton),
new
CommandBinding(ApplicationCommands.Cut, OnCutCommand));
CommandManager.RegisterClassInputBinding(typeof(MyButton),
new
InputBinding(ApplicationCommands.Cut,
new
KeyGesture(Key.X,
ModifierKeys.Control)));
CommandManager.RegisterClassCommandBinding(typeof(MyButton),
new
CommandBinding(ApplicationCommands.Paste, OnPasteCommand));
CommandManager.RegisterClassInputBinding(typeof(MyButton),
new
InputBinding(ApplicationCommands.Paste,
new
KeyGesture(Key.V,
ModifierKeys.Control)));
}
public MyButton()
{
Focusable =
true;
Loaded += (ss, ee)
=> this.Focus();
}
//控件内命令执行
protected
virtual
void OnCut()
{
Clipboard.SetText(Content.ToString());
Content =
null;
}
protected
virtual
void OnPaste()
{
Content =
Clipboard.GetText();
}
//CommandBinding命令绑定事件方法
private
static
void OnCutCommand(object sender,
ExecutedRoutedEventArgs e)
{
var control
= sender
as MyButton;
if (control
!= null)
{
control.OnCut();
}
}
private
static
void OnPasteCommand(object sender,
ExecutedRoutedEventArgs e)
{
var control
= sender
as MyButton;
if (control
!= null)
{
control.OnPaste();
}
}
}
然后在界面上,可以直接用Ctrl+X和Ctrl+V对按钮内容进行剪切粘贴(当然前提是按钮得有焦点),也可以用其他控件(ICommandSource执行者)来触发针对自定义按钮的命令(通过设置ICommandSource.CommandTarget)。
XAML:
<StackPanel>
<loc:MyButton x:Name="mybtn"
Height="50">Mgen!</loc:MyButton>
<Button Command="Cut"
CommandTarget="{Binding ElementName=mybtn}">剪切</Button>
<Button Command="Paste"
CommandTarget="{Binding ElementName=mybtn}">粘贴</Button>
</StackPanel>
下面自定义一个控件,继承与Button类型,使得按钮中的文字支持剪切和粘贴。在自定义类型的静态构造函数中用RegisterClassCommandBinding把剪切和粘贴的命令(对应ApplicationCommands类型的Cut和Paste属性)执行实现。用RegisterClassInputBinding则定义命令执行的快捷键。
代码:
class
MyButton :
Button
{
static MyButton()
{
//使用CommandManager.RegisterClassCommandBinding和RegisterClassInputBinding方法
//注册我们需要的剪切和粘贴命令。
CommandManager.RegisterClassCommandBinding(typeof(MyButton),
new
CommandBinding(ApplicationCommands.Cut, OnCutCommand));
CommandManager.RegisterClassInputBinding(typeof(MyButton),
new
InputBinding(ApplicationCommands.Cut,
new
KeyGesture(Key.X,
ModifierKeys.Control)));
CommandManager.RegisterClassCommandBinding(typeof(MyButton),
new
CommandBinding(ApplicationCommands.Paste, OnPasteCommand));
CommandManager.RegisterClassInputBinding(typeof(MyButton),
new
InputBinding(ApplicationCommands.Paste,
new
KeyGesture(Key.V,
ModifierKeys.Control)));
}
public MyButton()
{
Focusable =
true;
Loaded += (ss, ee)
=> this.Focus();
}
//控件内命令执行
protected
virtual
void OnCut()
{
Clipboard.SetText(Content.ToString());
Content =
null;
}
protected
virtual
void OnPaste()
{
Content =
Clipboard.GetText();
}
//CommandBinding命令绑定事件方法
private
static
void OnCutCommand(object sender,
ExecutedRoutedEventArgs e)
{
var control
= sender
as MyButton;
if (control
!= null)
{
control.OnCut();
}
}
private
static
void OnPasteCommand(object sender,
ExecutedRoutedEventArgs e)
{
var control
= sender
as MyButton;
if (control
!= null)
{
control.OnPaste();
}
}
}
然后在界面上,可以直接用Ctrl+X和Ctrl+V对按钮内容进行剪切粘贴(当然前提是按钮得有焦点),也可以用其他控件(ICommandSource执行者)来触发针对自定义按钮的命令(通过设置ICommandSource.CommandTarget)。
XAML:
<StackPanel>
<loc:MyButton x:Name="mybtn"
Height="50">Mgen!</loc:MyButton>
<Button Command="Cut"
CommandTarget="{Binding ElementName=mybtn}">剪切</Button>
<Button Command="Paste"
CommandTarget="{Binding ElementName=mybtn}">粘贴</Button>
</StackPanel>
相关文章推荐
- 使用 ClientScriptManager 类的 RegisterClientScriptBlock方法添加客户端脚本
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- 通过使用ScriptManager.RegisterStartupScript,呈现后台多次使用alert方法
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- UpdatePanel中使用脚本文件(ScriptManager.RegisterStartupScript 方法)
- wpf Command Binding使用
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- 关于tableview的 [self.tableView registerClass: forCellReuseIdentifier:使用方法
- WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法
- WPF: 使用CommandManager.InvalidateRequerySuggested手动更新Command状态
- WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法
- WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
- WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
- ScriptManager.RegisterStartupScript方法
- 在VISTA中,使用RUNAS命令运行IE7时报错错误“The RUNAS command is not supported”的解决方法
- iOS 使用class_copyPropertyList等runtime方法的注意事项
- 使用WPF实现MDI窗体的方法
- WPF入门教程系列(二) 深入剖析WPF Binding的使用方法
- C# 使用 Environment.GetCommandLineArgs 方法 制作*.exe和参数 供其他程序调用
- 在WPF中使用WinForm控件方法