Back to Devexpress

SchedulerControl.AppointmentEditing Event

wpf-devexpress-dot-xpf-dot-scheduling-dot-schedulercontrol-036a90a3.md

latest12.8 KB
Original Source

SchedulerControl.AppointmentEditing Event

Occurs before the user applies changes to appointments.

Namespace : DevExpress.Xpf.Scheduling

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

NuGet Package : DevExpress.Wpf.Scheduling

Declaration

csharp
public event AppointmentEditingEventHandler AppointmentEditing
vb
Public Event AppointmentEditing As AppointmentEditingEventHandler

Event Data

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

PropertyDescription
CancelGets or sets whether the event should be canceled. Inherited from CancelRoutedEventArgs.
CanceledEditAppointmentsProvides access to the collection of appointments for which changes should be canceled.
ConflictedAppointmentsReturns the collection of edited appointments that are conflicting with the current appointments.
EditAppointmentsProvides access to the collection of edited 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.
SourceAppointmentsProvides access to the collection of appointments before the changes the user attempts to apply.

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 edited, add them to the CanceledAppointments collection.

The event’s SourceAppointments property returns the collection of appointments before the changes the user attempts to apply. If the event’s Cancel property is set to false , the edited appointments stored in the EditAppointments collection are saved to SchedulerControl.AppointmentItems.

To implement custom editing, set the event’s Cancel property to true and manually populate the AppointmentEditingEventArgs.SourceAppointments collection.

The code snippet below demonstrates how to implement validation:

csharp
AppointmentEditing += (d, e) => { 
    foreach(var apt in e.EditAppointments) { //each appointment which is about to be edited
        bool res = Validate(apt); //is validated by a custom method 
        if(!res) //if the validation fails 
            e.CanceledEditAppointments.Add(apt); //the changes are not applied
    } 
}
vb
AddHandler AppointmentEditing, Sub(d, e)
    For Each apt In e.EditAppointments 'each appointment which is about to be edited
        Dim res As Boolean = Validate(apt) 'is validated by a custom method
        If Not res Then 'if the validation fails
            e.CanceledEditAppointments.Add(apt) 'the changes are not applied
        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