/// <summary>Initializes a new instance of the <see cref="T:CommandBinding"/> class.</summary> /// <param name="control">The <see cref="T:ICommandControl"/> that can be bound to a <see cref="T:ICommand"/>.</param> public CommandBinding(ICommandControl control) { if (control == null) throw new ArgumentNullException("control"); _control = control; control.Activated += HandlerControlActivated; control.Disposed += HandlerControlDisposed; control.SetCommandEnabled(false); }
/// <summary> /// (自动)绑定命令按钮的ControlEvent 到关联的命令对象 /// </summary> /// <param name="control"></param> public void BindCommandControls(ICommandControl control) { object dataSource = GetInstanceByMemberName(control.CommandObject); string[] propNames = control.CommandName.Split('.'); object obj = GetPropertyValue(dataSource, propNames); IMvvmCommand command = obj as IMvvmCommand; BindCommandControls(control, control.ControlEvent, command); }
protected void RegisterControl( ICommandControl control ) { if( _commandControlSet == null ) { _deferredControls.Add( control ); } else { _commandControlSet.AddControl( control ); } }
/// <summary> /// 绑定一个命令控件到一个有参数的命令方法上 /// </summary> /// <typeparam name="T">命令方法的参数类型</typeparam> /// <param name="control">命令控件</param> /// <param name="command">带参数的命令方法</param> public void BindCommandControls <T>(ICommandControl control, CommandMethod <T> command) { //dictCommand.Add(control, command); EventHandler hander = new EventHandler( (object sender, EventArgs e) => { CommandEventMethod <T>(sender, e, command); }); Type ctrType = control.GetType(); ctrType.GetEvent(control.ControlEvent).AddEventHandler(control, hander); }
/// <summary> /// 绑定控件的事件到命令接口对象 /// </summary> /// <param name="control">窗体控件</param> /// <param name="controlEvent">控件的事件</param> /// <param name="command">要绑定的命令接口对象</param> public void BindCommandControls(object control, string controlEvent, IMvvmCommand command) { EventHandler hander = new EventHandler( (object sender, EventArgs e) => { object paraValue = null; if (control is ICommandControl) { try { ICommandControl cmdCtr = control as ICommandControl; if (cmdCtr.ParameterObject != null && cmdCtr.ParameterProperty != null) { object paraSource = GetInstanceByMemberName(cmdCtr.ParameterObject); string[] paraPropNames = cmdCtr.ParameterProperty.Split('.'); paraValue = GetPropertyValue(paraSource, paraPropNames); } } catch (Exception ex) { RaiseBinderError(control, ex); return; } } if (command.BeforExecute(paraValue)) { try { command.Execute(paraValue); } catch (Exception ex) { RaiseBinderError(control, ex); } finally { command.AfterExecute(); } } }); Type ctrType = control.GetType(); ctrType.GetEvent(controlEvent).AddEventHandler(control, hander); }
private void CommandEventMethod <T>(object sender, EventArgs e, CommandMethod <T> command) { ICommandControl cmdCtr = sender as ICommandControl; try { //这里不处理命令控件关联的命令对象 object dataSource = GetInstanceByMemberName(cmdCtr.ParameterObject); T paraValue = GetCommandParameterValue <T>(dataSource, cmdCtr.ParameterProperty); //CommandMethod<T> method = (CommandMethod<T>)dictCommand[sender]; //method(paraValue); command(paraValue); } catch (Exception ex) { RaiseBinderError(sender, ex); } }
/// <summary> /// Binds this object to any <see cref="CommandToolStripMenuItem"/>s and <see cref="CommandToolStripButton"/>s /// that are found in the specified <see cref="ToolStripItemCollection"/>. /// </summary> public void BindTo(ToolStripItemCollection toolStripItems, IMainWindow mainWindow) { Util.ThrowIfParameterNull(toolStripItems, "toolStripItems"); foreach (ToolStripItem item in toolStripItems) { ICommandControl commandControl = item as ICommandControl; if (commandControl != null) { BindTo(commandControl, mainWindow); } ToolStripDropDownItem dropDownItem = item as ToolStripDropDownItem; if (dropDownItem != null) { BindTo(dropDownItem.DropDownItems, mainWindow); } } }
/// <summary> /// Bind this object to a single control /// </summary> public void BindTo(ICommandControl control, IMainWindow mainWindow) { ((ICommand)control.Command).SetMainWindow(mainWindow); control.SelectionBroadcaster = this; }