How to drag and drop appointment to the exact hour in WPF Scheduler (Calendar)
You can drag the appointment to the exact hour in WPF SfScheduler by using the AppointmentEditFlag property and AppointmentDropping event in SfScheduler.
C#
Create a ViewModel class and add the appointment details.
public class SchedulerViewModel : NotificationObject
{
private ScheduleAppointmentCollection events;
private ObservableCollection<object> resources;
private List<string> eventNames;
public SchedulerViewModel()
{
CreateResources();
CreateResourceAppointments();
}
private void CreateResourceAppointments()
{
Events = new ScheduleAppointmentCollection();
Random randomTime = new Random();
List<Point> randomTimeCollection = this.GettingTimeRanges();
var resurceCollection = this.Resources as ObservableCollection<object>;
Events.Add(new ScheduleAppointment()
{
StartTime = dateTime1,
EndTime = dateTime1.AddHours(2),
Subject = this.eventNames[randomTime.Next(4)],
ResourceIdCollection = new ObservableCollection<object>() { scheduleResource.Id },
});
}
}
}
private void CreateResources()
{
Resources = new ObservableCollection<object>()
{
new SchedulerResource() { Name = "Sophia", Id = "1000" },
new SchedulerResource() { Name = "Zoey Addison", Id = "1001" },
new SchedulerResource() { Name = "James William", Id = "1002" },
};
}
}
XAML
Resources can be added to the scheduler by setting the ResourceGroupType property as Resource in SfScheduler. Enable appointment drag and drop-in schedule by setting the AppointmentEditFlag property of SfScheduler to DragDrop.
<Window.DataContext>
<local:SchedulerViewModel/>
</Window.DataContext>
<Grid>
<syncfusion:SfScheduler x:Name="Schedule"
ViewType="Week"
ResourceGroupType="Resource"
ResourceCollection="{Binding Resources}"
ItemsSource="{Binding Events}"
AppointmentEditFlag="DragDrop" >
<interactivity:Interaction.Behaviors>
<local:SchedulerBehavior/>
</interactivity:Interaction.Behaviors>
</syncfusion:SfScheduler>
</Grid>
C#
Initialize an event handler for AppointmentDropping event of Scheduler. In AppointmentDropping event handler, get the drop time of appointment using the DropTime property of event, round off the value to exact hour by mentioning the minute value to zero and assign the rounded off value to the DropTime property of event.
public class SchedulerBehavior : Behavior<SfScheduler>
{
SfScheduler scheduler;
protected override void OnAttached()
{
base.OnAttached();
scheduler = this.AssociatedObject;
this.AssociatedObject.AppointmentDropping += AssociatedObject_AppointmentDropping;
}
private void AssociatedObject_AppointmentDropping(object sender, AppointmentDroppingEventArgs e)
{
var dropTime = e.DropTime;
e.DropTime = new DateTime(dropTime.Year, dropTime.Month, dropTime.Day, dropTime.Hour, 0, 0);
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.AppointmentDropping -= AssociatedObject_AppointmentDropping;
this.scheduler = null;
}
}
