Skip to main content

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

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

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;
}

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:

[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);
}

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:

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