Skip to main content

Command Factory Service

  • 3 minutes to read

The ISchedulerCommandFactoryService service allows you to replace default Scheduler commands inside the Bar and Ribbon UI.

View Example

  1. Create a custom command class inherited from the default Scheduler command you want to replace. The Default Scheduler commands are stored in the DevExpress.XtraScheduler.Commands namespace. In the sample code below, a custom command derives from the SplitAppointmentOperationCommand class.

    public class CustomSplitAppointmentOperationCommand : SplitAppointmentOperationCommand
        {
            public CustomSplitAppointmentOperationCommand(ISchedulerCommandTarget target) : base(target) { }
            public CustomSplitAppointmentOperationCommand(SchedulerControl control) : base(control) { }
    
            private TimeSpan splitInterval = TimeSpan.FromMinutes(10);
            public TimeSpan SplitAppointmentCommandStep
            {
                get
                {
                    return splitInterval;
                }
                set
                {
                    splitInterval = value;
                }
            }
            protected override IOperation CreateOperation()
            {
                //TimeScaleCollection timeScales = CreateTimeScaleCollection();
                TimeScaleCollection timeScales = new TimeScaleCollection();
                timeScales.Add(new TimeScaleFixedInterval(SplitAppointmentCommandStep));
                return new SplitAppointmentOperation(SchedulerControl, timeScales, SchedulerControl.SelectedAppointments[0]);
            }
        }
    
  2. Create a custom class that implements the ISchedulerCommandFactoryService interface. This interface exposes the ISchedulerCommandFactoryService.CreateCommand method you should implement. This method identifies commands by IDs and initializes the corresponding command instances.

    public class CustomSchedulerCommandFactoryService : ISchedulerCommandFactoryService
        {
            readonly ISchedulerCommandFactoryService service;
            readonly SchedulerControl control;
    
            public CustomSchedulerCommandFactoryService(SchedulerControl control,
                ISchedulerCommandFactoryService service)
            {
                Guard.ArgumentNotNull(control, "control");
                Guard.ArgumentNotNull(service, "service");
                this.control = control;
                this.service = service;
            }
    
            #region ISchedulerCommandFactoryService Members
            //implement this method to create custom command instances
            public SchedulerCommand CreateCommand(SchedulerCommandId id)
            {
                //check the command id to return an instance of the correct class
                if (id == SchedulerCommandId.SplitAppointment)
                {
                    CustomSplitAppointmentOperationCommand cmd = new CustomSplitAppointmentOperationCommand(control);
                    cmd.SplitAppointmentCommandStep = TimeSpan.FromHours(12);
                    return cmd;
                }
                return service.CreateCommand(id);
            }
            #endregion
        }
    
  3. Call the SchedulerControl.RemoveService method to remove the default service from the SchedulerControl.Services collection, and use the SchedulerControl.AddService method to add the replacement service.

    private void Form1_Load(object sender, EventArgs e) {
                //...
                CustomSchedulerCommandFactoryService commandFactory =
                new CustomSchedulerCommandFactoryService(schedulerControl1, schedulerControl1.GetService<ISchedulerCommandFactoryService>());
                schedulerControl1.RemoveService(typeof(ISchedulerCommandFactoryService));
                schedulerControl1.AddService(typeof(ISchedulerCommandFactoryService), commandFactory);
            }