Lesson 2 - Provide Resources for Appointments
- 4 minutes to read
This topic describes how to add resources to a scheduling RIA Services application, and how to link these resources to appointments. In this example, we will use the CarsDB local SQL Server 2008 R2 Express database's Cars table as resources for the scheduler.
To provide a scheduling application with resources, do the following.
- Step 1. Modify Entity Data Model
- Step 2. Update Domain Service
- Step 3. Bind the Scheduler To Data
- Step 4. Specify Mappings
- Step 5. Customize the Scheduler
- Result
#Modify Entity Data Model
- Open the Silverlight application with the SchedulerControl, which was created in Lesson 1 of the current Getting Started section.
The SchedulerControl is already bound to the CarScheduling table of the CarsDB local SQL Server 2008 R2 Express database via RIA Services (the CarsDB.mdf file is placed in C:\Users\Public\Documents\DevExpress Demos 14.2\Components\Silverlight\CS\SchedulerDemo.Web\App_Data by default). To bind the scheduler to the Cars table of the same database, the first step requires you to add an entity corresponding to this table to the existing entity data model. To do this, double-click CarsDBModel.edmx in the Solution Explorer, right-click CarsDBModer.edmx in the invoked Model Browser and select Update Model from Database... from the context menu.
In Update Wizard, select the Cars table and click Finish.
The diagram representing the updated entity data model will look like it is shown below.
- Save and rebuild the solution.
#Update the Domain Service
Open the SchedulingDomainService.cs file and add the GetCars query method that returns resource data to the SchedulingDomainService class.
- Rebuild the solution.
#Bind the Scheduler to Data
Open the MainPage.xaml file (the SilverlightAplication1 project).
In XAML markup, define the DomainDataSource control to load resource data from the domain context. Specify SchedulingDomainContext for the DomainDataSource.DomainContext property and set the DomainDataSource.QueryName property value to the GetCarsQuery query method that has been previously added to the specified domain service to be used for loading resource data. Note that query method name can be with or without a "Query" suffix.
Update the MainPage XAML in the following way.
<UserControl.Resources> <riaControls:DomainDataSource Name="domainDataSource1" x:Key="appointmentsDomainDataSource" AutoLoad="True" LoadedData="DomainDataSource_LoadedData" QueryName="GetCarSchedulingQuery" SubmittedChanges="DomainDataSource_SubmittedChanges"> <riaControls:DomainDataSource.DomainContext> <local:SchedulingDomainContext/> </riaControls:DomainDataSource.DomainContext> </riaControls:DomainDataSource> <!--Insert this block--> <riaControls:DomainDataSource x:Key="resourcesDomainDataSource" AutoLoad="True" LoadedData="DomainDataSource_LoadedData" QueryName="GetCarsQuery"> <riaControls:DomainDataSource.DomainContext> <local:SchedulingDomainContext/> </riaControls:DomainDataSource.DomainContext> </riaControls:DomainDataSource> <!--End of the block--> </UserControl.Resources>
To bind the SchedulerControl to resource data, you should provide this data to the ResourceStorage object, which is returned by the SchedulerStorage.ResourceStorage property. To do this, set the ResourceStorage.DataSource property to the resourcesDomainDataSource domain data source.
<dxsch:SchedulerStorage.ResourceStorage> <dxsch:ResourceStorage DataSource="{Binding Source={StaticResource ResourceKey=resourcesDomainDataSource}, Path=Data}"/> </dxsch:SchedulerStorage.ResourceStorage>
Now you need to map all required data fields for scheduler resources.
#Specify Mappings
First, map the ResourceId property of an appointment. This property specifies the unique identifier of the appointment's associated resource. Then, specify standard mappings for resource properties.
<dxsch:SchedulerControl Name="schedulerControl1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ActiveViewType="Week">
<dxsch:SchedulerControl.Storage>
<dxsch:SchedulerStorage AppointmentsChanged="SchedulerStorage_AppointmentsModified"
AppointmentsInserted="SchedulerStorage_AppointmentsModified"
AppointmentsDeleted="SchedulerStorage_AppointmentsModified">
<dxsch:SchedulerStorage.AppointmentStorage>
<dxsch:AppointmentStorage
DataSource="{Binding Source={StaticResource ResourceKey=appointmentsDomainDataSource}, Path=Data}">
<dxsch:AppointmentStorage.Mappings>
<!--Update this block-->
<dxsch:AppointmentMapping
Start="StartTime"
End="EndTime"
AllDay="AllDay"
Description="Description"
Label="Label"
Location="Location"
Subject="Subject"
RecurrenceInfo="RecurrenceInfo"
ReminderInfo="ReminderInfo"
ResourceId="CarId"
Status="Status"
Type="EventType"/>
<!--End of the block-->
</dxsch:AppointmentStorage.Mappings>
</dxsch:AppointmentStorage>
</dxsch:SchedulerStorage.AppointmentStorage>
<dxsch:SchedulerStorage.ResourceStorage>
<dxsch:ResourceStorage
DataSource="{Binding Source={StaticResource ResourceKey=resourcesDomainDataSource}, Path=Data}">
<!--Insert this block-->
<dxsch:ResourceStorage.Mappings>
<dxsch:ResourceMapping Id="ID" Caption="Model" />
</dxsch:ResourceStorage.Mappings>
<!--End of the block-->
</dxsch:ResourceStorage>
</dxsch:SchedulerStorage.ResourceStorage>
</dxsch:SchedulerStorage>
</dxsch:SchedulerControl.Storage>
</dxsch:SchedulerControl>
#Step 5. Customize the Scheduler
Group Appointments by Resources
To group the scheduler's data by resources, set the SchedulerControl.GroupType property value to Resource in XAML.
<dxsch:SchedulerControl Name="schedulerControl1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ActiveViewType="Week" GroupType="Resource"/>
Set the Number of Shown Resources
In our example, there are a lot of resources in the ResourceStorage. This makes the scheduler's area very crowded. To avoid this, you can use the SchedulerViewBase.ResourcesPerPage property to specify the number of resources shown on the screen simultaneously.
Set the ResourcesPerPage property value to 3 in XAML.
<dxsch:SchedulerControl.WeekView> <dxsch:WeekView ResourcesPerPage="3"></dxsch:WeekView> </dxsch:SchedulerControl.WeekView>
#Result
Run the project. The following image shows the resulting SchedulerControl at runtime.