Hi,
Maybe this will help you guys:
The purpose of these attached properties are to select a template for special days (like for example bold the days of the month that have appointments or color holidays from a database). MVVM friendly.
In the viewmdodel we have:
We populate these dates:
in xaml we have the datatemplate for special days:
and the RadCalendar is defined in xaml:
The final pieces of the puzzle are the custom daytemplateselector and the attached properties:
public class SpecialDaySelector : DataTemplateSelector
{
private List<DateTime> _specialDates;
private DataTemplate _specialDayTemplate { get; set; }
public SpecialDaySelector(List<DateTime> specialDates, DataTemplate specialDayTemplate)
{
_specialDates = specialDates;
_specialDayTemplate = specialDayTemplate;
}
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var content = item as CalendarButtonContent;
if (content != null)
{
if (_specialDates.Contains(content.Date))
{
return _specialDayTemplate;
}
}
return base.SelectTemplate(item, container);
}
}
public class RadCalendarBehavior : DependencyObject
{
private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var calendar = d as RadCalendar;
if (e.NewValue != null)
{
calendar.DayTemplateSelector = new SpecialDaySelector((List<DateTime>)e.NewValue, GetSpecialDayTemplate(d));
}
}
public static List<DateTime> GetSpecialDays(DependencyObject obj)
{
return (List<DateTime>)obj.GetValue(SpecialDaysProperty);
}
public static void SetSpecialDays(DependencyObject obj, List<DateTime> value)
{
obj.SetValue(SpecialDaysProperty, value);
}
public static readonly DependencyProperty SpecialDaysProperty =
DependencyProperty.RegisterAttached("SpecialDays", typeof(List<DateTime>), typeof(RadCalendarBehavior), new UIPropertyMetadata(null, OnSpecialDaysChanged));
public static DataTemplate GetSpecialDayTemplate(DependencyObject obj)
{
return (DataTemplate)obj.GetValue(SpecialDayTemplateProperty);
}
public static void SetSpecialDayTemplate(DependencyObject obj, DataTemplate value)
{
obj.SetValue(SpecialDayTemplateProperty, value);
}
public static readonly DependencyProperty SpecialDayTemplateProperty =
DependencyProperty.RegisterAttached("SpecialDayTemplate", typeof(DataTemplate), typeof(RadCalendarBehavior), new UIPropertyMetadata(null));
}
Hope it helps.