public static ToolStripItem CreateItem(this IMenuDef def, ResourceManager resourcemanager, Func <object> commandParameterCallback = null) { ToolStripItem elem = null; if (def.MenuType == MenuType.Seperator) { elem = new ToolStripSeparator(); } else { elem = new ToolStripMenuItem(); elem.Text = def.Text; if (!string.IsNullOrEmpty(def.Image)) { elem.Image = (Image)resourcemanager.GetObject(def.Image); } if (def.ShortCutKeys > 0) { ((ToolStripMenuItem)elem).ShortcutKeys = (Keys)def.ShortCutKeys; } if (def.Command != null) { if (commandParameterCallback == null) { commandParameterCallback = () => null; } MenuItemCommandBinding binding = new MenuItemCommandBinding(elem as ToolStripMenuItem, def.Command, commandParameterCallback); } } elem.Name = def.Name; elem.Size = new System.Drawing.Size(def.SizeH, def.SizeV); return(elem); }
/// <summary> /// Builds a new command binding. /// </summary> /// <typeparam name="TView">The type of the view the new binding binds to.</typeparam> /// <typeparam name="TControl">The type of the control the new binding binds to.</typeparam> /// <typeparam name="TViewModel">The type of the viewmodel the new binding binds to.</typeparam> /// <param name="view">The view to bind to.</param> /// <param name="control">The control to bind to.</param> /// <param name="viewModel">The viewmodel to bind to.</param> /// <param name="viewModelProperty">The binding source property (the property on the viewmodel to bind to).</param> /// <exception cref="NotSupportedException">Thrown when an unsupported control is encountered.</exception> /// <returns>A new command binding.</returns> internal static Binding <TView> BuildCommand <TView, TControl, TViewModel>(TView view, TControl control, TViewModel viewModel, Expression <Func <TViewModel, ICommand> > viewModelProperty) where TViewModel : INotifyPropertyChanged { Binding <TView> result = null; // type of command binding is derived from the type of control if (typeof(TControl) == typeof(Button)) { result = new ButtonCommandBinding <TView, TViewModel>(view, control as Button, viewModel, viewModelProperty); } //if (typeof(TControl) == typeof(ToolStripMenuItem)) //{ // result = new ToolStripMenuItemCommandBinding<TView, TViewModel>(view, control as ToolStripMenuItem, // viewModel, viewModelProperty); //} //if (typeof(TControl) == typeof(ToolStripButton)) //{ // result = new ToolStripButtonCommandBinding<TView, TViewModel>(view, control as ToolStripButton, // viewModel, viewModelProperty); //} if (typeof(TControl) == typeof(MenuItem)) { result = new MenuItemCommandBinding <TView, TViewModel>(view, control as MenuItem, viewModel, viewModelProperty); } if (result != null) { result.HookEvents(); return(result); } // unsupported control type throw new NotSupportedException(string.Format("Command binding is not supported for {0}", typeof(TControl))); }