Change date colors in Rad calendar

21 posts, 0 answers
  1. shraddha
    shraddha avatar
    4 posts
    Member since:
    May 2009

    Posted 16 Oct 2009 Link to this post

    Hi,
    I am using Telerik WPF control rad calendar.
    I want to highlight some dated in that and  want to disable some dates in runtime. Is it possible?

    --Thanks
    Shraddha
  2. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 20 Oct 2009 Link to this post

    Hi shraddha,

    Consider using the DayTemplateSelector property of the RadCalendar. You can check our RadCalendar first look example, as it is showing these exact feature of the control. Let us know if you need any further assistance.

    Best wishes,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WPF is Visual Studio 2017 Ready
  4. shraddha
    shraddha avatar
    4 posts
    Member since:
    May 2009

    Posted 23 Oct 2009 Link to this post

    Hi,
    Thanx for the help.
    I tried using DayTemplateSelector and DataTemplate. And it is working fine.
    I have set of special dates which i want to update (add and remove dates from set) in runtime.
    How can i achieve that??

    ---Thanx.
  5. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 26 Oct 2009 Link to this post

    Hi shraddha,

    I am not sure if I am understanding correctly your request. Can you please explain deeply what do you mean by 'add and remove dates from set in runtime' as RadCalendar doesn't allow such operations.

    All the best,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. shraddha
    shraddha avatar
    4 posts
    Member since:
    May 2009

    Posted 26 Oct 2009 Link to this post

    Hi,
    In the example that you have given, You are selecting some dates (with binding calendar to some CustomdateSelection class). 
    Suppose those dates are 12 and 15. I want to change that set of date to 12, 15 and 20 and so on.... Like these can I add and remove dates from the set in the runtime???
    Let me know if you need any more clarification.
    -- Thanx.
  7. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 28 Oct 2009 Link to this post

    Hello shraddha,

    Now I am getting the idea of your request. Unfortunately this is not possible with the current version of the control. We will consider an improvement of the control in this direction for some of the future releases. I hope that this is not a show stopper for your application.

    Greetings,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Vytautas Smitas
    Vytautas Smitas avatar
    6 posts
    Member since:
    Aug 2009

    Posted 06 Nov 2009 Link to this post

    Hi shraddha,

    I don't know if I understood correctly, but when I want to refresh date coloured on runtime, I just update one List and when call a method which consits of these 3 lines:

    DataTemplateSelector dts = radCalendar1.DayTemplateSelector; 
    radCalendar1.DayTemplateSelector = new DisableWeekendsSelection(); 
    radCalendar1.DayTemplateSelector = dts; 
     My DataTemplateSelector class gets that List and recolours the dates.

    Maybe it's not a perfect solution, but it works and it works fast. :)
  9. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 12 Nov 2009 Link to this post

    Hi Vytautas Smitas,

    This is a possible solution. The only problem is that you need  to create a couple of different DayTemplateSelector based classes. We will think of some improvements of the RadCalendar on those  aspects.

    Regards,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  10. Viren
    Viren avatar
    4 posts
    Member since:
    Aug 2010

    Posted 27 Sep 2010 Link to this post

    Hi Everyone,

    I am facing a similar problem. I have like a list of dates that I need to highlight in the calendar. I fetched the list form sql server in the backend and store it in an array of DateTime type in my C# code behind. Its a WPF application. Now I want to keep  today's (current) date highlighted as it is, but also want to highlight this list of dates that I am fetching in the radcalendar. I tried adding the list to the SelectedDates property of the radcalendar but it that messes up my OnSelectionChanged event handler. Any solution as to how to highlight a few dates on the  calendar?

    Thanks
  11. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 30 Sep 2010 Link to this post

    Hi Viren,

    You have to use the DayTemplateSelector or DayTemplate property. There you can define the appearance of each calendar item in a class that implements the aforementioned classes . Find the referred example with the exact implementation.

    Sincerely yours,
    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
  12. Viren
    Viren avatar
    4 posts
    Member since:
    Aug 2010

    Posted 30 Sep 2010 Link to this post

    Thank you Kaloyan. I will try to implement this and will get back whenever I get a chance.
  13. Gabriel
    Gabriel avatar
    2 posts
    Member since:
    Jun 2009

    Posted 12 Nov 2010 Link to this post

    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:
    private List<DateTime> _datesWithAppointments;
            public List<DateTime> DatesWithAppointments
            {
                get
                {
                    return _datesWithAppointments;
                }
                set
                {
                    if (_datesWithAppointments != value)
                    {
                        _datesWithAppointments = value;
                        NotifyPropertyChanged("DatesWithAppointments");
                    }
                }
            }


    We populate these dates:
    DatesWithAppointments = Appointments.Select(x => x.Start.Date).ToList();


    in xaml we have the datatemplate for special days:
    <DataTemplate x:Key="SpecialDayTemplate">
                <TextBlock Text="{Binding Text}" FontWeight="Bold" />
    </DataTemplate>


    and the RadCalendar is defined in xaml:
    <telerik:RadCalendar Margin="0,10,0,0" x:Name="radCalendar" VerticalAlignment="Top" DisplayDate="{Binding MySelectedDate}" SelectedDate="{Binding MySelectedDate, Mode=TwoWay}" telerik:StyleManager.Theme="Office_Blue" local:RadCalendarBehavior.SpecialDayTemplate="{StaticResource SpecialDayTemplate}" local:RadCalendarBehavior.SpecialDays="{Binding DatesWithAppointments}" />


    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.
  14. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 16 Nov 2010 Link to this post

    Hello Gabriel,

    You can find a similar approach in our FirstLook demo example. And your workaround is acceptable too.

    Best wishes,
    Kaloyan
    the Telerik team
    See What's New in RadControls for WPF in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
  15. David
    David avatar
    19 posts
    Member since:
    Jun 2008

    Posted 02 Dec 2010 Link to this post

    Hi Gabriel,

    Thanks for posting a MVVM solution.

    It would be good if Telerik updated their examples for MVVM. It's so frustrating when they use a StaticResource in their examples, when what you really need is an example that allows you bind to your ViewModel.

    Dave
  16. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 07 Dec 2010 Link to this post

    Hello David,

    Thank you for the note. We will take the suggestion into account for the next update of the calendar examples.

    Regards,
    Kaloyan
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
  17. alexander rodriguez
    alexander  rodriguez avatar
    1 posts
    Member since:
    Dec 2009

    Posted 24 Dec 2010 Link to this post

    hi shraddha,

    Can you share me your DataTemplateSelector class. Thanks
  18. Kshamesh
    Kshamesh avatar
    16 posts
    Member since:
    Aug 2012

    Posted 09 Aug 2012 Link to this post

    Hello Gabriel,

    You solution is very perfect in my requirement. I would like to understand if i can select different data templates based on custom logic instead of returning SpecialDayTemplate each time. I have GreenDayTemplate, RedDayTemplate etc. To process this custom logic I have custom dateTime collection property.

    Your help in this regard would be much appreciated!

    Regards,
    Kshamesh
  19. Adrian
    Adrian avatar
    5 posts
    Member since:
    Dec 2011

    Posted 11 Nov 2013 Link to this post

    Telerik: 
    Have you guys incorporated an easier method to altering special dates as mentioned previously in this post or is the same approach still the recommendation?
  20. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 14 Nov 2013 Link to this post

    Hello,

    We haven't changed the way of altering the special dates. You will have to use DayTemplateSelector or DayTemplate property as suggested in one of the previous posts.

    Regards,
    Konstantina
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  21. Lerner
    Lerner avatar
    2 posts
    Member since:
    Dec 2013

    Posted 15 Aug 2014 in reply to Gabriel Link to this post

    Hello Gabriel

    Thanks for your solution, i try'd to implement it but i have a little problem that the special date are not shown when the window is loaded only after i change the view to year and go back or if i go t a different month and come back than it shows, so if you can help it should show up when the calender loads the first time.
     
    Thanks .
  22. Lerner
    Lerner avatar
    2 posts
    Member since:
    Dec 2013

    Posted 15 Aug 2014 in reply to Vytautas Smitas Link to this post

    Hello Vytautas Smitas

    Thanks for your solution can you please explain me more how this works, what is the DisableWeekendsSelection?

    Thanks.
Back to Top
UI for WPF is Visual Studio 2017 Ready