New to Telerik UI for WPF? Download free 30-day trial

Implementing View-ViewModel

The purpose of this tutorial is to show you how to bind a RadScheduleView with a ViewModel.

Before reading this tutorial you should get familiar with the Data Binding support of the RadScheduleView control.

  • Add a new RadScheduleView declaration in your XAML

<telerik:RadScheduleView /> 
  • Create a new class named MyViewModel.

public class MyViewModel 
{ 
} 
  • In the MyViewModel class add two properties:

  1. Appointments - we will bind the AppointmentsSource property of the RadScheduleView to this property.

  2. ResourcesTypes - we will bind the ResourceTypesSource property of the RadScheduleView to this property.

private ObservableCollection<Appointment> appointments; 
private ObservableCollection<ResourceType> resourceTypes; 
public ObservableCollection<Appointment> Appointments 
{ 
    get 
    { 
        return this.appointments; 
    } 
    set 
    { 
        this.appointments = value; 
    } 
} 
public ObservableCollection<ResourceType> ResourcesTypes 
{ 
    get 
    { 
        return this.resourceTypes; 
    } 
    set 
    { 
        this.resourceTypes= value; 
    } 
} 
  • Let's create a method in the ViewModel that generates some Resources:

private ObservableCollection<ResourceType> GenerateResourceTypes() 
{ 
    ObservableCollection<ResourceType> result = new ObservableCollection<ResourceType>(); 
 
    ResourceType roomType = new ResourceType("Room"); 
    Resource room102 = new Resource("Room 102"); 
    Resource room203 = new Resource("Room 203"); 
    Resource room406 = new Resource("Room 406"); 
    roomType.Resources.Add(room102); 
    roomType.Resources.Add(room203); 
    roomType.Resources.Add(room406); 
 
    ResourceType speakerType = new ResourceType("Speaker"); 
    Resource tomSpeaker = new Resource("Tom"); 
    Resource peterSpeaker = new Resource("Peter"); 
    speakerType.Resources.Add(tomSpeaker); 
    speakerType.Resources.Add(peterSpeaker); 
 
    result.Add(roomType); 
    result.Add(speakerType); 
    return result; 
} 
  • All we have to do is to initialize the resourceTypes and appointments fields:

public MyViewModel() 
{ 
    this.resourceTypes = this.GenerateResourceTypes(); 
    this.appointments = new ObservableCollection<Appointment>(); 
} 
  • The ViewModel is complete. Now, let's return to the View. Add some ViewDefinitions, GroupDescriptionsSource and bind the AppointmentsSource and ResourceTypes

<telerik:RadScheduleView AppointmentsSource="{Binding Appointments}"  
                         ResourceTypesSource="{Binding ResourcesTypes}" > 
    <telerik:RadScheduleView.ViewDefinitions> 
        <telerik:DayViewDefinition /> 
        <telerik:WeekViewDefinition /> 
        <telerik:TimelineViewDefinition /> 
    </telerik:RadScheduleView.ViewDefinitions> 
    <telerik:RadScheduleView.GroupDescriptionsSource> 
        <telerik:GroupDescriptionCollection> 
            <telerik:DateGroupDescription /> 
            <telerik:ResourceGroupDescription ResourceType="Speaker" /> 
            <telerik:ResourceGroupDescription ResourceType="Room" /> 
        </telerik:GroupDescriptionCollection> 
    </telerik:RadScheduleView.GroupDescriptionsSource> 
</telerik:RadScheduleView> 
  • Finally, set the DataContext:

this.DataContext = new MyViewModel(); 
In this article