Back to Devexpress

How to: Create Regular and Recurrent Appointments at the View Model Level

wpf-401629-controls-and-libraries-scheduler-examples-how-to-create-regular-and-recurrent-appointments-at-the-view-model-level.md

latest4.7 KB
Original Source

How to: Create Regular and Recurrent Appointments at the View Model Level

  • Feb 13, 2020
  • 2 minutes to read

View Example

This example illustrates how to add a new regular or recurrent appointment programmatically when the Scheduler is in bound mode.

Important

It’s essential that your data source type implements the INotifyCollectionChanged (e.g., ObservableCollection<T>). In this case, the Scheduler Control will receive notifications about its changes.

To add a new appointment, create a new data item instance, define its properties, and add it to your source. In this example, SchedulerControl’s SelectedInterval property is bound to the Interval property from the view model. Its values are used in the data item’s Start and End properties:

csharp
protected ApptViewModel CreateAppt(string subj, DateTime start, DateTime end, string description) {
    ApptViewModel apptViewModel = new ApptViewModel() {
        Subject = subj,
        Start = start,                
        End = end,
        Description = "[add description]"
    };
    return apptViewModel;
}
vb
Protected Function CreateAppt(ByVal subj As String, ByVal start As DateTime, ByVal [end] As DateTime, ByVal description As String) As ApptViewModel
    Dim apptViewModel As New ApptViewModel() With {
        .Subject = subj,
        .Start = start,
        .End = [end],
        .Description = "[add description]"
    }
    Return apptViewModel
End Function

Set the item’s Type property to AppoinementType.Pattern and define a corresponding recurrence rule in the RecurrenceInfo property to create a recurrent appointment. Use RecurrenceBuilder to generate this rule:

csharp
[Command]
public void AddAppt(bool recurrent = false) {
    var appt = CreateAppt($"New Appt #{Appointments.Count}", Interval.Start, Interval.End, "[add description]");

    if(recurrent) {
        appt.Type = (int)AppointmentType.Pattern;
        appt.RecurrenceInfo = RecurrenceBuilder.Daily(Interval.Start, 10).Build().ToXml();
    } else {
        appt.Type = (int)AppointmentType.Normal;
    }

    this.Appointments.Add(appt);
    this.SelectedAppointments.Clear();
    this.SelectedAppointments.Add(appt);
}
vb
<Command>
Public Sub AddAppt(Optional ByVal recurrent As Boolean = False)
    Dim appt = CreateAppt($"New Appt #{Appointments.Count}", Interval.Start, Interval.End, "[add description]")

    If recurrent Then
        appt.Type = CInt(Math.Truncate(AppointmentType.Pattern))
        appt.RecurrenceInfo = RecurrenceBuilder.Daily(Interval.Start, 10).Build().ToXml()
    Else
        appt.Type = CInt(Math.Truncate(AppointmentType.Normal))
    End If

    Me.Appointments.Add(appt)
    Me.SelectedAppointments.Clear()
    Me.SelectedAppointments.Add(appt)
End Sub

Refer to the How to: Create Recurrence in Code article for more information about generating recurrence rules.

This example also illustrates how you can invoke EditAppointmentWindow for a newly created appointment. This functionality is implemented with the help of the CompositeCommandBehavior class. The first CommandItem’s Command property is bound to a property from the view model. The second CommandItem’s Command is bound to SchedulerControl’s Commands.ShowAppointmentWindowCommand command:

xaml
<dxb:BarButtonItem Content="Add a regular appointment">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CompositeCommandBehavior CanExecuteCondition="AnyCommandCanBeExecuted">
            <dxmvvm:CommandItem Command="{Binding AddApptCommand}"
                                CommandParameter="false" />
            <dxmvvm:CommandItem CheckCanExecute="False"
                                Command="{Binding ElementName=scheduler,
                                                  Path=Commands.ShowAppointmentWindowCommand}" />
        </dxmvvm:CompositeCommandBehavior>
    </dxmvvm:Interaction.Behaviors>
</dxb:BarButtonItem>