This is a migrated thread and some comments may be shown as answers.

How to disable selection for several dates

9 Answers 371 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
hadass
Top achievements
Rank 1
hadass asked on 03 Jan 2012, 11:05 AM
Hi.
I'm using telerikInput:RadDatePicker to select a date but i need to limit the selection.
I have a list of unavailable dates (got pulled from the data base) that i don't want to allow the user to select. I want that when opening the DatePicker those dates will be disabled for selection.

what is the best way to do it?

Thanks, Hadass.

9 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 04 Jan 2012, 01:01 PM
Hello Hadass,

You should create a custom DayTemplateSelector and set it to the CalendarStyle of the DatePicker. I've attached a simple project to demonstrate the approach, please download it and examine it.

Regards,
Yana
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
hadass
Top achievements
Rank 1
answered on 05 Jan 2012, 03:54 PM
Hey Yana.
Thanks for your reply.

The code example you gave me is with Silverlight 4 and I'm using Silverlight 3.
is it doable in Silverlight 3 ?

(hence the "telerikInput" namespace : 

xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"

By the way, this is the namespace that i get when dragging the "RadDatePicker" control.)

In your example:
   <telerik:RadDatePicker>
            <telerik:RadDatePicker.CalendarStyle>
                <Style TargetType="telerik:RadCalendar">
                    <Setter Property="DayTemplateSelector" Value="{StaticResource CustomTemplateSelector}" />
                </Style>
            </telerik:RadDatePicker.CalendarStyle>
</telerik:RadDatePicker>

the property

<telerik:RadDatePicker.CalendarStyle>
 is just not available when using the telerikInput namespace.

Thanks for your reply.
Hadass.
0
Yana
Telerik team
answered on 05 Jan 2012, 04:36 PM
Hi Hadass,

Could you please tell us the exact version of the controls that you're using?

Greetings,
Yana
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
hadass
Top achievements
Rank 1
answered on 06 Jan 2012, 05:36 PM
Hi Yana

I checked the details in the properties of the Telerik.Windows.Controls.dll
The version is  2010.1.603.1030
The runtime version is  v2.0.50727

Is this the information you need?

Thanks, Hadass
0
Accepted
Yana
Telerik team
answered on 10 Jan 2012, 04:28 PM
Hello Hadass,

It seems that you're using too old version of the controls and the approach for disabling days is different. I've attached a new SL3 project  to demonstrate it, please download the attachment and try it.

Kind regards,
Yana
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
hadass
Top achievements
Rank 1
answered on 02 Feb 2012, 08:50 AM
Thanks for the solution. It helped a lot and solved my problem.

Hadass.
0
hadass
Top achievements
Rank 1
answered on 07 Feb 2012, 07:26 AM
Hi.
After using this solution I bump into another problem.
I have a list of invalid dates that I send to the costume selector, but this list updates dynamically. The problem is that the costume template selector doesn't refresh itself.

I looked up for solutions online but I couldn't find anything related to DatePicker, though I found a thread from Telerik's forums about Calendar control, that has the same problem.

http://www.telerik.com/community/forums/silverlight/calendar/redraw-refresh-radcalendar.aspx 

Is this the same bug that was on old versions of Calendar control?
if i'm using the Telerik and Silverlight versions that are mentioned above, is it possible to refresh the the costume day template?

I add here the template I use:
public class CustomTemplateSelector : DataTemplateSelector
    {
        private static List<DateTime> _invalidDays = new List<DateTime>();
 
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            CalendarButtonContent content = item as CalendarButtonContent;
            if (content != null)
            {
                foreach (DateTime day in _invalidDays)
                {
                    if (content.Date.Date == day | content.Date.Date < DateTime.Today)
                    {
                        content.IsEnabled = false;
                        return this.DisabledTemplate;
                    }
                }
 
            }
            return this.DefaultTemplate;
        }
 
        public static void SetInvalidDays(List<DateTime> invalidDays)
        {
            _invalidDays = invalidDays;
        }
 
        public DataTemplate DisabledTemplate
        {
            get;
            set;
        }
 
        public DataTemplate DefaultTemplate
        {
            get;
            set;
        }
 
 
        public CalendarButtonContent List { get; set; }
    }


and this is the call for it from the main page with the updated list of invalid dates:
private void ActivateDatePickerTemplate()
{
    List<DateTime> daysToDisable = new List<DateTime>();
    for (int i = 0; i < escortDates.Count; i++)
    {
        if (escortDates.ElementAt(i).Value >= limitPerDay)
        {
            daysToDisable.Add(escortDates.ElementAt(i).Key.Value);
        }
    }
    CustomTemplateSelector.SetInvalidDays(daysToDisable);
}

Thanks for your help.
0
Accepted
Yana
Telerik team
answered on 16 Feb 2012, 02:41 PM
Hello Hadass,

I am sorry for the delayed reply.

You just need to change _invalidDays collection in the CustomTemplateSelector to be of type ObservableCollection<DateTime> as it implements INotifyCollectionChanged interface.

I've attached the modified project, please download it and give it a try.

Kind regards,
Yana
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
hadass
Top achievements
Rank 1
answered on 26 Feb 2012, 08:17 AM
Hi.
i didn't try your solution - we managed to by-pass the probelm by changing the DateTimePicker - SelectableDateEnd to a value and back to null..

dtFromDate.SelectableDateEnd = DateTime.Today;
dtFromDate.SelectableDateEnd = null;
Tags
DatePicker
Asked by
hadass
Top achievements
Rank 1
Answers by
Yana
Telerik team
hadass
Top achievements
Rank 1
Share this question
or