wpf-401589-controls-and-libraries-scheduler-examples-how-to-validate-appointment-items-when-the-user-is-adding-or-editing-them.md
SchedulerControl provides the AppointmentAdding and AppointmentEditing events. You can use them to implement validation. This example illustrates how you can show a warning message to users when an appointment intersects the lunch time.
The lunch time is defined as a recurrent Time Region Item:
<dxsch:SchedulerControl.TimeRegionItems>
<dxsch:TimeRegionItem
Type ="Pattern"
Start="1/1/2019 13:00:00" End="1/1/2019 14:00:00"
RecurrenceInfo="{dxsch:RecurrenceDaily Start='1/1/2019 13:00:00', ByDay=WorkDays}"
BrushName="{x:Static dxsch:DefaultBrushNames.TimeRegion3Hatch}"/>
</dxsch:SchedulerControl.TimeRegionItems>
The validation logic is implemented in the SchedulerValidationService class which is a DialogService class descendant. If an appointment intersects the lunch time, the service displays a dialog window and allows the user to cancel changes either in all appointments or only in the conflicted appointments. Users can also click the Ignore button to override validation and save changes:
bool ProcessAppointments(IReadOnlyList<AppointmentItem> appts, IList<AppointmentItem> itemsToCancel) {
var toCancel = new List<AppointmentItem>();
foreach (var item in appts){
var range = new DateTimeRange(item.Start, item.End);
var startLunchRange = new DateTimeRange(item.Start.Date.AddHours(13), item.Start.Date.AddHours(14));
var endLunchRange = new DateTimeRange(item.End.Date.AddHours(13), item.End.Date.AddHours(14));
if (range.Intersect(startLunchRange).Duration.Ticks != 0
|| range.Intersect(endLunchRange).Duration.Ticks != 0
|| range.Duration.Hours > 23)
toCancel.Add(item);
}
if (toCancel.Count > 0) {
var Cancel = new UICommand() { Caption = "Cancel", IsDefault = true };
var CancelConflicts = new UICommand() { Caption = "Cancel Conflicts" };
var Ignore = new UICommand() { Caption = "Ignore", IsCancel = true };
var result = this.ShowDialog(new List<UICommand>() { Cancel, CancelConflicts, Ignore },
"Warning",
"WarningUserControl",
"The following appointment(-s) intersects the lunch time:\n\n"
+ string.Join("\n", toCancel.Select(c => c.Subject)) +
"\n\nClick 'Cancel' to discard all changes.\nClick 'Cancel Conflicts' to cancel changes only in these appointment(-s).");
if (result == Cancel)
return false;
if (result == CancelConflicts)
foreach (var item in toCancel)
itemsToCancel.Add(item);
}
return true;
}
Private Function ProcessAppointments(ByVal appts As IReadOnlyList(Of AppointmentItem), ByVal itemsToCancel As IList(Of AppointmentItem)) As Boolean
Dim toCancel = New List(Of AppointmentItem)()
For Each item In appts
Dim range = New DateTimeRange(item.Start, item.End)
Dim startLunchRange = New DateTimeRange(item.Start.Date.AddHours(13), item.Start.Date.AddHours(14))
Dim endLunchRange = New DateTimeRange(item.End.Date.AddHours(13), item.End.Date.AddHours(14))
If range.Intersect(startLunchRange).Duration.Ticks <> 0 OrElse range.Intersect(endLunchRange).Duration.Ticks <> 0 OrElse range.Duration.Hours > 23 Then
toCancel.Add(item)
End If
Next item
If toCancel.Count > 0 Then
Dim Cancel = New UICommand() With {
.Caption = "Cancel",
.IsDefault = True
}
Dim CancelConflicts = New UICommand() With {.Caption = "Cancel Conflicts"}
Dim Ignore = New UICommand() With {
.Caption = "Ignore",
.IsCancel = True
}
Dim result = Me.ShowDialog(New List(Of UICommand)() From {Cancel, CancelConflicts, Ignore}, "Warning", "WarningUserControl", "The following appointment(-s) intersects the lunch time:" & vbLf & vbLf & String.Join(vbLf, toCancel.Select(Function(c) c.Subject)) & vbLf & vbLf & "Click 'Cancel' to discard all changes." & vbLf & "Click 'Cancel Conflicts' to cancel changes only in these appointment(-s).")
If result Is Cancel Then
Return False
End If
If result Is CancelConflicts Then
For Each item In toCancel
itemsToCancel.Add(item)
Next item
End If
End If
Return True
End Function
When the ProcessAppointments method returns False , the e.Cancel property is set to True in the AppointmentAdding or AppointmentEditing event handlers. If the user chooses to cancel changes only for the conflicted appointments, these appointments are added to the e.CanceledAppointments or e.CanceledEditAppointments collections:
private void Scheduler_AppointmentAdding(object sender, AppointmentAddingEventArgs e) {
e.Cancel = !ProcessAppointments(e.Appointments, e.CanceledAppointments);
}
private void Scheduler_AppointmentEditing(object sender, AppointmentEditingEventArgs e) {
e.Cancel = !ProcessAppointments(e.EditAppointments, e.CanceledEditAppointments);
}
Private Sub Scheduler_AppointmentAdding(ByVal sender As Object, ByVal e As AppointmentAddingEventArgs)
e.Cancel = Not ProcessAppointments(e.Appointments, e.CanceledAppointments)
End Sub
Private Sub Scheduler_AppointmentEditing(ByVal sender As Object, ByVal e As AppointmentEditingEventArgs)
e.Cancel = Not ProcessAppointments(e.EditAppointments, e.CanceledEditAppointments)
End Sub