CommandBinding用法

.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>

posted on 2007-09-13 16:34  sizzle  阅读(553)  评论(0编辑  收藏  举报