.cs文件中用法
private void BindCommands()
{
//[OK]
CommandBinding cb = new CommandBinding(Commands.OK, OnOK);
this.CommandBindings.Add(cb);
this.OkButton.Command = Commands.OK;
//[CANCEL]
cb = new CommandBinding(Commands.Cancel, OnCancel);
this.CommandBindings.Add(cb);
this.CancelButton.Command = Commands.Cancel;
}
然后在构造函数中调用即可。
.xmal中用法
先在.cs文件中定义: public static RoutedCommand CloseCommand = new RoutedCommand();
<Window.CommandBindings>
<CommandBinding Command="path:fileName.CloseCommand" Executed="Click_OK"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="path:fileName.CloseCommand" Gesture="Escape"/>
<KeyBinding Command="path:fileName.CloseCommand" Gesture="Enter"/>
</Window.InputBindings>
本文详细介绍了在WPF中如何使用.cs文件进行命令绑定,包括创建CommandBinding实例并将其添加到CommandBindings集合,以及在XAML中定义RoutedCommand和关联的InputBindings,通过示例代码展示了具体的实现过程。

1万+

被折叠的 条评论
为什么被折叠?



