Refresh calendar (reselecting datatemplate)

6 posts, 1 answers
  1. Kasper Schou
    Kasper Schou avatar
    21 posts
    Member since:
    Jul 2009

    Posted 08 Apr 2011 Link to this post

    Hi Telerik,

    I'm having trouble getting the calendar to reselect template for the calendarbuttoncontent after initial load.

    I've found that if I "browse" the calendar (so that the content is not shown) and back again, the template is reapplied, using updated information in my behind lists of dates.

    I have created the following template selector:

    public class CalendarButtonTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            CalendarButtonContent content = item as CalendarButtonContent;
            if (content != null && content.ButtonType == CalendarButtonType.Date)
            {
                if (CentralCalendarWindow.CalendarViewItems != null && CentralCalendarWindow.CalendarViewItems.Exists(c => c.Date == content.Date))
                {
                    CalendarViewItem calendarViewItem = CentralCalendarWindow.CalendarViewItems.Find(c => c.Date == content.Date);
                    if (calendarViewItem.Holiday)
                    {
                        return HolidayTemplate;
                    }
                    else if (calendarViewItem.NoDistribution)
                    {
                        return NoDistributionTemplate;
                    }
                    else if (calendarViewItem.PartlyNoDistribution)
                    {
                        return PartlyNoDistributionTemplate;           
                    }
                }
            }
            return DefaultTemplate;
        }
        public DataTemplate DefaultTemplate { get; set; }
        public DataTemplate HolidayTemplate { get; set; }
        public DataTemplate NoDistributionTemplate { get; set; }
        public DataTemplate PartlyNoDistributionTemplate { get; set; }
    }

    In XAML i bind to the selector as a static resource like this:

    DayTemplateSelector="{StaticResource templateSelector}"

    And my template selection is like this:

    <this:CalendarButtonTemplateSelector x:Key="templateSelector"
          DefaultTemplate="{StaticResource dataTemplateDefault}"
          HolidayTemplate="{StaticResource dataTemplateHoliday}"
          NoDistributionTemplate="{StaticResource dataTemplateNoDistribution}"
          PartlyNoDistributionTemplate="{StaticResource dataTemplatePartlyNoDistribution}"/>


    Now the question - How can I explicitly have the calendar redo its template selection, like it does when the content gets redrawn when "browsing" the calendar.

    And what I would really want to do is to only redo the template selection on specific days if possible.

    Thanks in advance!
    Best regards,
    Kasper Schou
  2. Kasper Schou
    Kasper Schou avatar
    21 posts
    Member since:
    Jul 2009

    Posted 08 Apr 2011 Link to this post

    Just to clarify, the following code will reapply the template selection:

    DateTime current = _calendar.DisplayDate;
    _calendar.DisplayDate = current.AddYears(1);
    _calendar.DisplayDate = current;

    But obviously I'm not very fond of using the above in my solution :)

    /Kasper
  3. UI for WPF is Visual Studio 2017 Ready
  4. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 14 Apr 2011 Link to this post

    Hi Kasper Schou,

    Sorry for the late response. What you can do is as follows:

    var currentSelector = calendar.DayTemplateSelector;
    calendar.DayTemplateSelector = null;
    calendar.DayTemplateSelector = currentSelector;


    Kind regards,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Kasper Schou
    Kasper Schou avatar
    21 posts
    Member since:
    Jul 2009

    Posted 14 Apr 2011 Link to this post

    Hi Kaloyan,

    Thank you for the reply, this is somewhat more pretty and works just fine :)

    Best regards,
    Kasper Schou
  6. Andrew
    Andrew avatar
    12 posts
    Member since:
    Oct 2011

    Posted 26 Oct 2012 Link to this post

    I've tried doing this by resetting the DayTemplateSelector as suggested and it works to an extent, but I have found that, although though the day templates are recalculated, the day buttons become disabled until I manually change months. Changing the DisplayDate as Kasper once mentioned works and doesn't cause this issue. Do you have any suggestions on how to fix this problem? 
  7. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 31 Oct 2012 Link to this post

    Hi,

    I think this is the best approach for now. Hope it is acceptable for you.
    If you need further information please let us know.

    Regards,
    Konstantina
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
UI for WPF is Visual Studio 2017 Ready