Back to Devexpress

SchedulerControl.AppointmentAdding Event

wpf-devexpress-dot-xpf-dot-scheduling-dot-schedulercontrol-c8aa371e.md

latest12.0 KB
Original Source

SchedulerControl.AppointmentAdding Event

Occurs before the user has added appointments to the scheduler.

Namespace : DevExpress.Xpf.Scheduling

Assembly : DevExpress.Xpf.Scheduling.v25.2.dll

NuGet Package : DevExpress.Wpf.Scheduling

Declaration

csharp
public event AppointmentAddingEventHandler AppointmentAdding
vb
Public Event AppointmentAdding As AppointmentAddingEventHandler

Event Data

The AppointmentAdding event's data class is AppointmentAddingEventArgs. The following properties provide information specific to this event:

PropertyDescription
AppointmentsProvides access to the collection of appointments the user attempts to add to the scheduler.
CancelGets or sets whether the event should be canceled. Inherited from CancelRoutedEventArgs.
CanceledAppointmentsProvides access to the collection of appointments that should be excluded from being added to the scheduler.
ConflictedAppointmentsReturns the collection of appointments that are conflicting with the current appointments.
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

To prevent specific appointments from being added to the scheduler, add them to the CanceledAppointments collection.

The code snippet below demonstrates how to implement custom validation:

csharp
AppointmentAdding += (d, e) => { 
    foreach(var apt in e.Appointments) { //each appointment which is about to be added
        bool res = Validate(apt); //is validated by a custom method 
        if(!res) //if the validation fails 
            e.CanceledEditAppointments.Add(apt); //this appointment is not added to the scheduler
    } 
}
vb
AddHandler AppointmentAdding, Sub(d, e)
    For Each apt In e.Appointments 'each appointment which is about to be added
        Dim res As Boolean = Validate(apt) 'is validated by a custom method
        If Not res Then 'if the validation fails
            e.CanceledEditAppointments.Add(apt) 'this appointment is not added to the scheduler
        End If
    Next apt
End Sub

Example

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:

xaml
<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:

csharp
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;
}
vb
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:

csharp
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);
}
vb
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

See Also

How to: Validate Appointment Items when the User Is Adding or Editing Them

SchedulerControl Class

SchedulerControl Members

DevExpress.Xpf.Scheduling Namespace