Articles in this section
Category / Section

How to dynamically change resources in WPF Schedule

2 mins read

You can dynamically change the resources to other resources that are added to SfSchedule based on your requirement.

XAML

Create a WPF application and add the SfSchedule control by specifying the resource collection.

<syncfusion:SfSchedule x:Name="schedule1"
                        ScheduleType="Week"
                        Background="White" 
                        Margin="20"           
                        ScheduleResourceTypeCollection="{Binding ResourceCollection}"
                        DayHeaderOrder="OrderByResource">
</syncfusion:SfSchedule>

C#

The ScheduleResourceTypeCollection property of the SfSchedule control is bound to the local property.

ResourceCollection = new ObservableCollection<ResourceType>();
ResourceCollection.Add(new ResourceType() { TypeName = "TeamA" });
ResourceCollection.Add(new ResourceType() { TypeName = "TeamB" });
ResourceCollection.Add(new ResourceType() { TypeName = "TeamC" });
ResourceCollection.Add(new ResourceType() { TypeName = "TeamD" });
ResourceCollection.Add(new ResourceType() { TypeName = "TeamE" });

C#

The schedule appointments can be grouped based on the resources associated with them.

ScheduleAppointmentCollection tempcollection = new ScheduleAppointmentCollection();
ScheduleAppointment appointment1 = new ScheduleAppointment() { StartTime = DateTime.Now.Date.AddHours(4), EndTime = DateTime.Now.Date.AddHours(6), Subject = "Johny's Appointment", AppointmentBackground = new SolidColorBrush(Colors.SteelBlue) };
appointment1.ResourceCollection.Add(new Resource() { TypeName = "TeamA", ResourceName = "Johny" });
 
ScheduleAppointment appointment2 = new ScheduleAppointment() { StartTime = DateTime.Now.Date.AddHours(4), EndTime = DateTime.Now.Date.AddHours(6), Subject = "Neal's Appointment", AppointmentBackground = new SolidColorBrush(Colors.SlateBlue) };
appointment2.ResourceCollection.Add(new Resource() { TypeName = "TeamB", ResourceName = "Neal" });
 
ScheduleAppointment appointment3 = new ScheduleAppointment() { StartTime = DateTime.Now.Date.AddHours(4), EndTime = DateTime.Now.Date.AddHours(6), Subject = "Peter's Appointment", AppointmentBackground = new SolidColorBrush(Colors.MidnightBlue) };
appointment3.ResourceCollection.Add(new Resource() { TypeName = "TeamC", ResourceName = "Peter" });
 
ScheduleAppointment appointment4 = new ScheduleAppointment() { StartTime = DateTime.Now.Date.AddHours(4), EndTime = DateTime.Now.Date.AddHours(6), Subject = "Morgan's Appointment", AppointmentBackground = new SolidColorBrush(Colors.SlateGray) };
appointment4.ResourceCollection.Add(new Resource() { TypeName = "TeamD", ResourceName = "Morgan" });
 
ScheduleAppointment appointment5 = new ScheduleAppointment() { StartTime = DateTime.Now.Date.AddHours(4), EndTime = DateTime.Now.Date.AddHours(6), Subject = "Smith's Appointment", AppointmentBackground = new SolidColorBrush(Colors.OrangeRed) };
appointment5.ResourceCollection.Add(new Resource() { TypeName = "TeamE", ResourceName = "Smith" });
 
tempcollection.Add(appointment1);
tempcollection.Add(appointment2);
tempcollection.Add(appointment3);
tempcollection.Add(appointment4);
tempcollection.Add(appointment5);
 
this.schedule1.Appointments = tempcollection;
this.schedule1.ScheduleResourceTypeCollection = ResourceCollection;
 
this.schedule1.Resource = "TeamA";

C#

To demonstrate the Resource navigation in Schedule, the resources are added in Expander control. When you check or uncheck the Checkboxes in the Expander, it navigates to the corresponding resource and perform Insert or Remove operation based on the IsChecked value of the respective CheckBox.

private void johny_Checked(object sender, RoutedEventArgs e)
{
    if ((bool)(sender as CheckBox).IsChecked)
    {
        ResourceCollection[0].ResourceCollection.Insert(0,new Resource() { ResourceName = "Johny", DisplayName = "Johny" });
    }
    else
    {
        var _resourceItem = ResourceCollection[0].ResourceCollection.FirstOrDefault(item => (item as Resource).DisplayName == "Johny");
        if (_resourceItem != null)
        {
            ResourceCollection[0].ResourceCollection.RemoveAt(ResourceCollection[0].ResourceCollection.IndexOf(_resourceItem));
        }
    }
    this.schedule1.Resource = string.Empty;
    this.schedule1.ScheduleResourceTypeCollection = ResourceCollection;
    this.schedule1.Resource = "TeamA";
}

C#

The Button.Click event can also be used to navigate through resources.

private void one_Click(object sender, RoutedEventArgs e)
{
    this.schedule1.Resource = "TeamA";
}
 
private void two_Click(object sender, RoutedEventArgs e)
{
    this.schedule1.Resource = "TeamB";
}
 
private void three_Click(object sender, RoutedEventArgs e)
{
    this.schedule1.Resource = "TeamC";
}
 
private void four_Click(object sender, RoutedEventArgs e)
{
    this.schedule1.Resource = "TeamD";
}
 
private void five_Click(object sender, RoutedEventArgs e)
{
    this.schedule1.Resource = "TeamE"; ;
}

View sample in GitHub

WPFSchedule Dynamic resource navigation

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied