Category / Section
How to access schedule events in MVVM by using commads
1 min read
You can handle schedule events in MVVM by using commands. This article explains how to use schedule events in MVVM by using commands in the schedule control.
Creating commands in SfScheduleCommands file:
- Create commands for needed schedule events in the schedule control for accessing those events in the MVVM. We have created commands for below listed schedule events in the SfScheduleCommands class file.
- AppointmentEditorOpening
- AppointmentEditorClosing
- ContextMenuOpening
- ContextMenuClosed
- VisibleDatesChanging
- We have used below code snippet to create commands for the above events.
C#
#region Base public class SfScheduleCommandBase<TBehavior> : ControlCommandBase<TBehavior, Syncfusion.UI.Xaml.Schedule.SfSchedule> where TBehavior : CommandBehaviorBase<Syncfusion.UI.Xaml.Schedule.SfSchedule>, new() { } public class SfScheduleCommandBehaviorBase<TReturn, TEventArgs> : BuilderCommandBehaviorBase<Syncfusion.UI.Xaml.Schedule.SfSchedule, TEventArgs, TReturn> { } #endregion #region AppointmentEditorOpening public class AppointmentEditorOpeningCommand<T, TBehavior> : SfScheduleCommandBase<TBehavior> where TBehavior : AppointmentEditorOpeningCommandBehavior<T>, new() { } public class AppointmentEditorOpeningCommandBehavior<TReturn> : SfScheduleCommandBehaviorBase<TReturn, AppointmentEditorOpeningEventArgs> { public AppointmentEditorOpeningCommandBehavior(Func<object, AppointmentEditorOpeningEventArgs, TReturn> builder) { this.builder = builder; } public AppointmentEditorOpeningCommandBehavior() : this(null) { } protected override void OnTargetAttached() { TargetObject.AppointmentEditorOpening += OnEventRaised; } } public class AppointmentEditorOpeningCommand : SfScheduleCommandBase<AppointmentEditorOpeningCommandBehavior> { } public class AppointmentEditorOpeningCommandBehavior : AppointmentEditorOpeningCommandBehavior<object> { } public class AppointmentEditorOpeningCommandWithEventArgs : AppointmentEditorOpeningCommand<AppointmentEditorOpeningEventArgs, AppointmentEditorOpeningCommandBehaviorEventArgs> { } public class AppointmentEditorOpeningCommandBehaviorEventArgs : AppointmentEditorOpeningCommandBehavior<AppointmentEditorOpeningEventArgs> { public AppointmentEditorOpeningCommandBehaviorEventArgs() : base((o, e) => e) { } } #endregion #region AppointmentEditorClosing public class AppointmentEditorClosingCommand<T, TBehavior> : SfScheduleCommandBase<TBehavior> where TBehavior : AppointmentEditorClosingCommandBehavior<T>, new() { } public class AppointmentEditorClosingCommandBehavior<TReturn> : SfScheduleCommandBehaviorBase<TReturn, AppointmentEditorClosedEventArgs> { public AppointmentEditorClosingCommandBehavior(Func<object, AppointmentEditorClosedEventArgs, TReturn> builder) { this.builder = builder; } public AppointmentEditorClosingCommandBehavior() : this(null) { } protected override void OnTargetAttached() { TargetObject.AppointmentEditorClosed += OnEventRaised; } } public class AppointmentEditorClosingCommand : SfScheduleCommandBase<AppointmentEditorClosingCommandBehavior> { } public class AppointmentEditorClosingCommandBehavior : AppointmentEditorClosingCommandBehavior<object> { } public class AppointmentEditorClosingCommandWithEventArgs : AppointmentEditorClosingCommand<AppointmentEditorClosedEventArgs, AppointmentEditorClosingCommandBehaviorEventArgs> { } public class AppointmentEditorClosingCommandBehaviorEventArgs : AppointmentEditorClosingCommandBehavior<AppointmentEditorClosedEventArgs> { public AppointmentEditorClosingCommandBehaviorEventArgs() : base((o, e) => e) { } } #endregion #region ContextMenuOpening public class ContextMenuOpeningCommand<T, TBehavior> : SfScheduleCommandBase<TBehavior> where TBehavior : ContextMenuOpeningCommandBehavior<T>, new() { } public class ContextMenuOpeningCommandBehavior<TReturn> : SfScheduleCommandBehaviorBase<TReturn, ContextMenuOpeningEventArgs> { public ContextMenuOpeningCommandBehavior(Func<object, ContextMenuOpeningEventArgs, TReturn> builder) { this.builder = builder; } public ContextMenuOpeningCommandBehavior() : this(null) { } protected override void OnTargetAttached() { TargetObject.ContextMenuOpening += OnEventRaised; } } public class ContextMenuOpeningCommand : SfScheduleCommandBase<ContextMenuOpeningCommandBehavior> { } public class ContextMenuOpeningCommandBehavior : ContextMenuOpeningCommandBehavior<object> { } public class ContextMenuOpeningCommandWithEventArgs : ContextMenuOpeningCommand<ContextMenuOpeningEventArgs, ContextMenuOpeningCommandBehaviorEventArgs> { } public class ContextMenuOpeningCommandBehaviorEventArgs : ContextMenuOpeningCommandBehavior<ContextMenuOpeningEventArgs> { public ContextMenuOpeningCommandBehaviorEventArgs() : base((o, e) => e) { } } #endregion #region ContextMenuClosed public class ContextMenuClosingCommand<T, TBehavior> : SfScheduleCommandBase<TBehavior> where TBehavior : ContextMenuClosingCommandBehavior<T>, new() { } public class ContextMenuClosingCommandBehavior<TReturn> : SfScheduleCommandBehaviorBase<TReturn, ContextMenuClosedEventArgs> { public ContextMenuClosingCommandBehavior(Func<object, ContextMenuClosedEventArgs, TReturn> builder) { this.builder = builder; } public ContextMenuClosingCommandBehavior() : this(null) { } protected override void OnTargetAttached() { TargetObject.ContextMenuClosed += OnEventRaised; } } public class ContextMenuClosingCommand : SfScheduleCommandBase<ContextMenuClosingCommandBehavior> { } public class ContextMenuClosingCommandBehavior : ContextMenuClosingCommandBehavior<object> { } public class ContextMenuClosingCommandWithEventArgs : ContextMenuClosingCommand<ContextMenuClosedEventArgs, ContextMenuClosingCommandBehaviorEventArgs> { } public class ContextMenuClosingCommandBehaviorEventArgs : ContextMenuClosingCommandBehavior<ContextMenuClosedEventArgs> { public ContextMenuClosingCommandBehaviorEventArgs() : base((o, e) => e) { } } #endregion #region VisibleDatesChanging public class VisibleDatesChangingCommand<T, TBehavior> : SfScheduleCommandBase<TBehavior> where TBehavior : VisibleDatesChangingCommandBehavior<T>, new() { } public class VisibleDatesChangingCommandBehavior<TReturn> : SfScheduleCommandBehaviorBase<TReturn, VisibleDatesChangingEventArgs> { public VisibleDatesChangingCommandBehavior(Func<object, VisibleDatesChangingEventArgs, TReturn> builder) { this.builder = builder; } public VisibleDatesChangingCommandBehavior() : this(null) { } protected override void OnTargetAttached() { TargetObject.VisibleDatesChanging += OnEventRaised; } } public class VisibleDatesChangingCommand : SfScheduleCommandBase<VisibleDatesChangingCommandBehavior> { } public class VisibleDatesChangingCommandBehavior : VisibleDatesChangingCommandBehavior<object> { } public class VisibleDatesChangingCommandWithEventArgs : VisibleDatesChangingCommand<VisibleDatesChangingEventArgs, VisibleDatesChangingCommandBehaviorEventArgs> { } public class VisibleDatesChangingCommandBehaviorEventArgs : VisibleDatesChangingCommandBehavior<VisibleDatesChangingEventArgs> { public VisibleDatesChangingCommandBehaviorEventArgs() : base((o, e) => e) { } } #endregion
- Create commands using ICommand interface in the MVVM view model for handling schedule evnets. We have used below code snippet for creating commands to handle the schedule events by using the ICommand interface.
C#
#region SfSchedule event commands #region Appointment Editor Opening ICommand _editorOpeningCommand; public ICommand EditorOpeningCommand { get { return _editorOpeningCommand; } } public void editorOpening(AppointmentEditorOpeningEventArgs arg) { } #endregion #region AppointmentEditor Closed ICommand _editorClosedCommand; public ICommand EditorClosedCommand { get { return _editorClosedCommand; } } public void EditorClosed(AppointmentEditorClosedEventArgs arg) { } #endregion #region ContextMenu Opening Event ICommand _contextmenuOpening; public ICommand ContextMenuOpeningCommand { get { return _contextmenuOpening; } } public void ContextmenuOpening(ContextMenuOpeningEventArgs arg) { MessageBox.Show("Triggered from VM and Context Menu Opening Event was cancelled"); } #endregion #region Context menu Closed ICommand _contextmenuClosing; public ICommand ContextMenuClosingCommand { get { return _contextmenuClosing; } } public void ContextmenuClosing(ContextMenuClosedEventArgs arg) { MessageBox.Show("Triggered from VM and Context Menu closed Event was Triggerd"); } #endregion #region Visible Date Changing ICommand _visibleDateChanging; public ICommand VisibleDateChangingCommand { get { return _visibleDateChanging; } } public void VisibleDateChanging(VisibleDatesChangingEventArgs arg) { var oldValue = arg.OldValue; var newValue = arg.NewValue; if (oldValue != null) MessageBox.Show("Visible date EventTriggered from VM"); } #endregion #endregion public ScheduleViewModel() { _editorOpeningCommand = new DelegateCommand<AppointmentEditorOpeningEventArgs>(editorOpening); _editorClosedCommand = new DelegateCommand<AppointmentEditorClosedEventArgs>(EditorClosed); _contextmenuOpening = new DelegateCommand<ContextMenuOpeningEventArgs>(ContextmenuOpening); _contextmenuClosing = new DelegateCommand<ContextMenuClosedEventArgs>(ContextmenuClosing); _visibleDateChanging = new DelegateCommand<VisibleDatesChangingEventArgs>(VisibleDateChanging); }
- In the above code we have initialized instance for each command by using DelegateCommand class which is in the Syncfusion.windows.shared library.
- Now create key for accessing SfScheduleCommands from the assembly namespace as we have created below.
XAML
xmlns:ScheduleCommand="clr-namespace:SfSchedule_MVVM_WPF.Commands"
- Now bind the schedule event commands in the MVVM view model to the corresponding commands which we have created in the SfScheduleCommands file. We have used below code snippet for binding the schedule event commands.
XAML
<syncfusion:SfSchedule ScheduleType="Week" TimeInterval="OneHour" x:Name="schedule" ItemsSource="{Binding ScheduleAppointmentCollection}" ScheduleCommand:AppointmentEditorOpeningCommandWithEventArgs.Command="{Binding EditorOpeningCommand}" ScheduleCommand:AppointmentEditorClosingCommandWithEventArgs.Command="{Binding EditorClosedCommand}" ScheduleCommand:ContextMenuOpeningCommandWithEventArgs.Command="{Binding ContextMenuOpeningCommand}" ScheduleCommand:ContextMenuClosingCommandWithEventArgs.Command="{Binding ContextMenuClosingCommand}" ScheduleCommand:VisibleDatesChangingCommandWithEventArgs.Command="{Binding VisibleDateChangingCommand}"/>
Now we can trigger the schedule events in MVVM view model by using commands.
Sample Link:
Note:
In this KB we have created a commands for the events available in SfSchedule in sample level. In any of our future release it will included inside the Syncfusion library and it can be used directly in sample just by referring the corresponding library.