I am trying to use the DayTemplateSelector to Select special days on the Calendar but I am having problems because I am getting my data (dates) from Ria Service. The Asynchronous nature of obtaining data makes it difficult to get the dates for SelectTemplate method . Any help on this will be highly appreciated. Below is part of the code:
public class CustomDayTemplateSelector : DataTemplateSelector
{
private WarrantyContext _Context = null;
private List<Inspection> _Inpections;
public CustomDayTemplateSelector()
{
_Context = new WarrantyContext();
_Inpections = new List<Inspection>();
LoadOperation<Inspection> loadOp = _Context.Load(_Context.GetInspectionsQuery());
loadOp.Completed += new EventHandler(LoadAppointments_Completed);
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
CalendarButtonContent content = item as CalendarButtonContent;
foreach (var a in _Inpections)
{
if (a.StartDate == content.Date)
{
return this.ActivityTemplate;
}
}
return DefaultTemplate;
}
public DataTemplate DefaultTemplate { get; set; }
public DataTemplate ActivityTemplate { get; set; }
private void LoadAppointments_Completed(object sender, EventArgs e)
{
foreach (var inspection in _Context.Inspections)
{
Inspection appointment = new Inspection();
appointment.StartDate = inspection.StartDate;
appointment.EndDate = inspection.EndDate;
appointment.UserId = inspection.UserId;
_Inpections.Add(appointment);
}
}
}
public class CustomDayTemplateSelector : DataTemplateSelector
{
private WarrantyContext _Context = null;
private List<Inspection> _Inpections;
public CustomDayTemplateSelector()
{
_Context = new WarrantyContext();
_Inpections = new List<Inspection>();
LoadOperation<Inspection> loadOp = _Context.Load(_Context.GetInspectionsQuery());
loadOp.Completed += new EventHandler(LoadAppointments_Completed);
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
CalendarButtonContent content = item as CalendarButtonContent;
foreach (var a in _Inpections)
{
if (a.StartDate == content.Date)
{
return this.ActivityTemplate;
}
}
return DefaultTemplate;
}
public DataTemplate DefaultTemplate { get; set; }
public DataTemplate ActivityTemplate { get; set; }
private void LoadAppointments_Completed(object sender, EventArgs e)
{
foreach (var inspection in _Context.Inspections)
{
Inspection appointment = new Inspection();
appointment.StartDate = inspection.StartDate;
appointment.EndDate = inspection.EndDate;
appointment.UserId = inspection.UserId;
_Inpections.Add(appointment);
}
}
}