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

Red Calendar Day / Special Dates

4 Answers 240 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
thorsten wand
Top achievements
Rank 1
thorsten wand asked on 04 Sep 2009, 12:58 PM
Hi,
I have a question regarding red calendar days(special days). There are video tutorials for winforms and silverlight which use red calendar days. Unfortunately I couldn't find a collection of Special Days in the WPF RadCalendar Control property section in Visual Studio 2008.
How can I bind a DateCollection as Special Days to the RadCalendar Control?

Thanks in advance

tWand

4 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 08 Sep 2009, 07:49 AM
Hi thorsten wand,

The RadCalendar control is not exposing such a collection you can use to add some Special Days. To achieve scenario like the one above, you can use DayTemplateSelector property.
    <telerikInput:RadCalendar x:Name="calendar" IsTodayHighlighted="true" 
                            SelectionChanged="OnCalendarSelectionChanged" DayTemplate="{x:Null}" 
                            HorizontalAlignment="Center" VerticalAlignment="Center" 
                            SelectionMode="Extended" Columns="3" ViewsHeaderVisibility="Visible" 
                            Culture="en-US"
                        <telerikInput:RadCalendar.DayTemplateSelector>  
                            <my:DisableWeekendsSelection> 
                                <my:DisableWeekendsSelection.DefaultTemplate>  
                                    <DataTemplate> 
                                        <TextBlock Text="{Binding Text}" /> 
                                    </DataTemplate> 
                                </my:DisableWeekendsSelection.DefaultTemplate> 
                                <my:DisableWeekendsSelection.SpecialDay> 
                                    <DataTemplate> 
                                        <TextBlock Text="{Binding Text}" Foreground="Red"/> 
                                    </DataTemplate> 
                                </my:DisableWeekendsSelection.SpecialDay> 
                            </my:DisableWeekendsSelection> 
                        </telerikInput:RadCalendar.DayTemplateSelector> 
                    </telerikInput:RadCalendar> 
 
public class DisableWeekendsSelection : DataTemplateSelector 
    { 
        public override DataTemplate SelectTemplate(object item, DependencyObject container) 
        { 
            CalendarButtonContent content = item as CalendarButtonContent; 
 
            if (content != null
            { 
                if (content.Date.DayOfWeek == DayOfWeek.Saturday || content.Date.DayOfWeek == DayOfWeek.Sunday) 
                { 
                    content.IsEnabled = false
                } 
            }  
            if (content.Date.Day % 11 == 0 && content.ButtonType == CalendarButtonType.Date) 
            { 
                return SpecialDay; 
            } 
 
            return DefaultTemplate; 
        } 
 
        private DataTemplate defaultTemplate; 
        public DataTemplate DefaultTemplate 
        { 
            get 
            { 
                return defaultTemplate; 
            } 
            set 
            { 
                defaultTemplate = value; 
            } 
        } 
 
        private DataTemplate specialDay; 
        public DataTemplate SpecialDay 
        { 
            get 
            { 
                return specialDay; 
            } 
            set 
            { 
                this.specialDay = value; 
            } 
        } 
    } 
 
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.
0
arman
Top achievements
Rank 1
answered on 25 Nov 2018, 03:10 PM

I can't find DayTemplateSelector in Ver: 2018.3 !

plz upload Sample.

0
Dinko | Tech Support Engineer
Telerik team
answered on 27 Nov 2018, 12:27 PM
Hello Arman,

The DayTemplateSelector property hasn't been removed from the control. I have double check this with the latest version of our controls and this property is working as expected. Can you elaborate more on what do you mean by you can't find this selector property?

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
arman
Top achievements
Rank 1
answered on 12 Dec 2018, 04:52 PM

i fix it

 https://drive.google.com/file/d/1dsr3fDRxbTOdNwCg6A6njLJ54KS04iNI/view?usp=sharing

DCalander.xaml file code:

<UserControl
             xmlns:local="clr-namespace:ArmanCustomControl.CustomControl.Calander"
             x:Class="ArmanCustomControl.CustomControl.Calander.DCalander"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
            
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>

        <local:DayButtonTemplateSelector x:Key="DayButtonTemplateSelector" >
            <local:DayButtonTemplateSelector.DefaultTemplate >
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding Icon}"  Foreground="{Binding IconBrush}" FontFamily="DeltaFont Solid" FontSize="32" TextAlignment="Center"/>

                        <TextBlock Text="{Binding Text}" FontSize="22" FontFamily="B Yekan" TextAlignment="Center" />
                    </Grid>
                </DataTemplate>
            </local:DayButtonTemplateSelector.DefaultTemplate>
            <!--<local:DayButtonTemplateSelector.BookedDayTemplate>
                <DataTemplate >
                    <Grid>
                        <TextBlock Text="{Binding Icon}"  Foreground="{Binding IconBrush}" FontFamily="DeltaFont Solid" FontSize="32" TextAlignment="Center"/>
                    
         
                        <TextBlock Text="{Binding Text}"
                                   Margin="0 6 0 0"
                                   Foreground="Black"
                                   FontWeight="Bold"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"/>
                    </Grid>
                </DataTemplate>
            </local:DayButtonTemplateSelector.BookedDayTemplate>-->
            <!--<local:DayButtonTemplateSelector.SpecialHolidayTemplate>
                <DataTemplate>
                    <Grid>

                        <TextBlock Text="{Binding Icon}" />
                        
            --><!--<Path Data="M11.5,0 L14.2,8.3 L23.0,8.3 L16,13.5 L18.6,22 L11.5,16.7 L4.4,22 L7.1,13.5 L0,8.3 L8.7,8.3 z"
                              Fill="#FF25A0DA"
                              Height="34"
                              Stretch="Fill"
                              UseLayoutRounding="False"
                              Width="34"
                              Opacity="0.4"/>-->
            <!--
                        <TextBlock Text="{Binding Text}"
                                   Margin="0 5 0 0"
                                   Foreground="Black"
                                   FontWeight="Bold"
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center"/>
                    </Grid>
                </DataTemplate>
            </local:DayButtonTemplateSelector.SpecialHolidayTemplate>-->
        </local:DayButtonTemplateSelector>
    </UserControl.Resources>

    <Grid>

        <telerik:RadCalendar x:Name="uxRadCalander" DayTemplateSelector="{StaticResource DayButtonTemplateSelector}"/>

    </Grid>
</UserControl>

namespace ArmanCustomControl.CustomControl.Calander
{
    public class DayButtonTemplateSelector : DataTemplateSelector
    {

        #region Function

        private int _haveDateCounter = 0;
        private List<DateTime> _haveDateList = new List<DateTime>();
        public bool HaveDate(DateTime currentDate)
        {
            if (_haveDateCounter > 67)//اگر به 67 رسید ریستارت میشه
            {
                _haveDateCounter = 0;
            }
            if (_haveDateList.Contains(currentDate))//اگر در لیست بود
            {
                return true;
            }
            else
            {
                if (_haveDateList.Count < 68) // تعداد داده ها در لیست کمتر از 68 داده باشه
                {
                    _haveDateList.Add(currentDate);
                }
                else
                {
                    _haveDateList[_haveDateCounter] = currentDate;
                }

            }

            _haveDateCounter++;
            return false;
        }
        #endregion
        public sealed class CalendarSpecialDay : CalendarButtonContent
        {

            #region Function
            internal void SetDate(DateTime date)//set datetime Private Date Property Value in CalendarButtonContent
            {
                if (date == DateTime.MinValue)
                {
                    return;
                }
                ArmanUiHelper.PropertyHelper.SetPrivatePropertyValue(this, "Date", date);
            }
            internal void SetButtonType(Telerik.Windows.Controls.Calendar.CalendarButtonType buttonType)//set datetime Private ButtonType Property Value in CalendarButtonContent
            {
                if (this.Date == DateTime.MinValue)
                {
                    return;
                }
                ArmanUiHelper.PropertyHelper.SetPrivatePropertyValue(this, "ButtonType", buttonType);
            }

            #endregion

            #region Property
            public System.Windows.Media.Brush IconBrush { get; set; }//book Day Color
            public DateTime BookDate { get; set; }//Book date

            public string Icon { get; set; }

            public string ToolTip { get; set; }
            #endregion
            #region Constractor
            public CalendarSpecialDay()
            {
                IconBrush = ArmanUiHelper.Helper.ToBrushD(System.Drawing.Color.DarkGray);
                Icon = "";
                ToolTip = "";

            }
            public CalendarSpecialDay(DateTime specialDate,string imageItem,string toolTip,Brush imageColor = null)
            {
                if (imageColor == null)
                {
                    imageColor = ArmanUiHelper.Helper.ToBrushD(System.Drawing.Color.DarkGray);
                }
                IconBrush = imageColor;
                Icon = imageItem;
                ToolTip = toolTip;
                BookDate = specialDate;

            }
            #endregion

        }
        public DataTemplate DefaultTemplate { get; set; }
        
        private static List<CalendarSpecialDay> _specialHolidays = new List<CalendarSpecialDay>();
        public List<CalendarSpecialDay> SpecialHolidays { get { return _specialHolidays; } set { _specialHolidays = value; } }

        public DayButtonTemplateSelector()
        {
            this.SpecialHolidays = new List<CalendarSpecialDay>();
        }
        public DayButtonTemplateSelector( List<DayButtonTemplateSelector.CalendarSpecialDay> specialDay = null)
        {
           
            if (specialDay == null)
            {
                specialDay = new List<CalendarSpecialDay>();
            }
            

            if (this.SpecialHolidays == null)
            {
                this.SpecialHolidays = new List<CalendarSpecialDay>();
            }

            this.SpecialHolidays.AddRange(specialDay);


        }

        private DateTime _curentDatetime;
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            //if (item is CalendarSpecialDay && ((CalendarSpecialDay) item).Date == DateTime.MinValue)
            //{

            //    return this.DefaultTemplate;
            //}
            System.Windows.Controls.ContentPresenter contentPresenter = container as System.Windows.Controls.ContentPresenter;
            if (contentPresenter != null)
            {


                Telerik.Windows.Controls.Calendar.CalendarButton calendarButtonContent = (contentPresenter.TemplatedParent as Telerik.Windows.Controls.Calendar.CalendarButton);
                
                CalendarButtonContent currentCalanderCuntent = (CalendarButtonContent) calendarButtonContent.Content;
                if (_curentDatetime != null && _curentDatetime == currentCalanderCuntent.Date)
                {
                    return DefaultTemplate;
                }
           
                _curentDatetime = currentCalanderCuntent.Date;
                CalendarSpecialDay contentPresenterContent = new CalendarSpecialDay();
                bool haveDateinCalander = HaveDate(currentCalanderCuntent.Date);
                //if (!haveDateinCalander)
                //{
                    contentPresenterContent.SetDate(currentCalanderCuntent.Date);
                    contentPresenterContent.SetButtonType(calendarButtonContent.ButtonType);
               // }

                //   contentPresenterContent.DateD = calendarButtonContent.Date;
                contentPresenterContent.Text = calendarButtonContent.Text;
                //  contentPresenterContent.ButtonTypeD = calendarButtonContent.ButtonType;

                contentPresenterContent.IsEnabled = calendarButtonContent.IsEnabled;
                contentPresenterContent.IsFromCurrentView = calendarButtonContent.IsFromCurrentView;
                contentPresenterContent.IsSelected = calendarButtonContent.IsSelected;
                contentPresenterContent.IsInAnotherView = calendarButtonContent.IsInAnotherView;


                contentPresenterContent.Icon = "";
                contentPresenterContent.ToolTip = "";
                if ((contentPresenter.Content is CalendarSpecialDay))
                {
                    var forEqul = ((CalendarSpecialDay)contentPresenter.Content);
                    if (
                        forEqul.IsEnabled != contentPresenterContent.IsEnabled &&
                        forEqul.IsFromCurrentView != contentPresenterContent.IsFromCurrentView &&
                        forEqul.IsInAnotherView != contentPresenterContent.IsInAnotherView &&
                        forEqul.IsSelected != contentPresenterContent.IsSelected &&
                        forEqul.ButtonType != contentPresenterContent.ButtonType &&
                        forEqul.Date != contentPresenterContent.Date &&
                        forEqul.Icon != contentPresenterContent.Icon &&
                        forEqul.IconBrush != contentPresenterContent.IconBrush &&
                        forEqul.ToolTip != contentPresenterContent.ToolTip &&
                        forEqul.Text != contentPresenterContent.Text &&
                        !haveDateinCalander// و قبلا محاسبه نشده بود
                    )
                    {
                        contentPresenter.Content = contentPresenterContent;
                        contentPresenter.DataContext = contentPresenterContent;
                    }
                }



                if (this.SpecialHolidays.Any(a => a.BookDate.ToShortDateString() == contentPresenterContent.Date.ToShortDateString()))
                {
                    var dateItem = this.SpecialHolidays.FirstOrDefault(a =>
                        a.BookDate.ToShortDateString() == contentPresenterContent.Date.ToShortDateString());
                    if (dateItem != null)
                    {
                        contentPresenterContent.Icon = dateItem.Icon;
                        contentPresenterContent.ToolTip = dateItem.ToolTip;
                        if (contentPresenterContent.Date != DateTime.MinValue)
                        {

                            contentPresenter.Content = contentPresenterContent;
                            contentPresenter.DataContext = contentPresenterContent;
                        }
                        else
                        {

                            contentPresenterContent.SetDate(currentCalanderCuntent.Date);
                            contentPresenterContent.SetButtonType(calendarButtonContent.ButtonType);
                            contentPresenter.Content = contentPresenterContent;
                            contentPresenter.DataContext = contentPresenterContent;
                        }

                    }
                    else
                    {

                        contentPresenterContent.Icon = "";
                        contentPresenterContent.ToolTip = "";

                        if (contentPresenterContent.Date != DateTime.MinValue)
                        {

                            contentPresenter.Content = contentPresenterContent;
                            contentPresenter.DataContext = contentPresenterContent;
                        }
                        else
                        {

                            contentPresenterContent.SetDate(currentCalanderCuntent.Date);
                            contentPresenterContent.SetButtonType(calendarButtonContent.ButtonType);
                            contentPresenter.Content = contentPresenterContent;
                            contentPresenter.DataContext = contentPresenterContent;
                        }

                    }

                }
                else //اگه رزرو نشده بود
                {
                    contentPresenterContent.Icon = "";
                    contentPresenterContent.ToolTip = "";
                    if (contentPresenterContent.Date != DateTime.MinValue)
                    {

                        contentPresenter.Content = contentPresenterContent;
                        contentPresenter.DataContext = contentPresenterContent;
                    }
                    else
                    {

                        contentPresenterContent.SetDate(currentCalanderCuntent.Date);
                        contentPresenterContent.SetButtonType(calendarButtonContent.ButtonType);
                        contentPresenter.Content = contentPresenterContent;
                        contentPresenter.DataContext = contentPresenterContent;
                    }
                }


            }


            return this.DefaultTemplate;
        }
      
    }

    #region Model

    public class CalendarDay
    {
        public string Image { get; set; }//تصویر از دلتا فونت
        public string ToolTip { get; set; }//توضیحات
        public bool IsRecurring { get; set; }//تکرار شود
    }
    #endregion

    public partial class DCalander : UserControl
    {
        #region Property
        List<DateTime> _specialDays = new List<DateTime>();

        public List<DayButtonTemplateSelector.CalendarSpecialDay> SpecialDays
        {
            get
            {
                if (Resources.Contains("DayButtonTemplateSelector") == false)
                {
                    return new List<DayButtonTemplateSelector.CalendarSpecialDay>();
                }
                DayButtonTemplateSelector template = (DayButtonTemplateSelector)Resources["DayButtonTemplateSelector"];
                return template.SpecialHolidays;

            }
            set
            {
                if (Resources.Contains("DayButtonTemplateSelector") == false)
                {
                    return;
                }
              
                Resources["DayButtonTemplateSelector"] = new
                    DayButtonTemplateSelector( value);
            }
        }

        public CultureInfo Culture
        {
            get { return uxRadCalander.Culture; }
            set
            {
                if (value.Name.ToLower() == "fa-ir")
                {
                    var culture = new CultureInfo("fa-Ir");
                    culture = PersianCultureHelper.FixPersianCulture(culture, FixCultureOptions.foptAll);
                    uxRadCalander.Culture = culture;

                }
                else
                {
                    uxRadCalander.Culture = value;
                }

            }
        }

        public DateTime? DisplayDateEnd
        {
            get { return uxRadCalander.DisplayDateEnd; }
            set { uxRadCalander.DisplayDateEnd = value; }
        }
        public DateTime? DisplayDateStart
        {
            get { return uxRadCalander.DisplayDateStart; }
            set { uxRadCalander.DisplayDateStart = value; }
        }

        #endregion

        #region Constractor
        public DCalander()
        {
            InitializeComponent();
        }
        #endregion


    }
}

 

namespace ArmanUiHelper
{
    /// <summary>
    /// جهت دسترسی به پروپرتی های مخفی اشیاء
    /// </summary>
    public static class PropertyHelper
    {
        /// <summary>
        /// Returns a _private_ Property Value from a given Object. Uses Reflection.
        /// Throws a ArgumentOutOfRangeException if the Property is not found.
        /// </summary>
        /// <typeparam name="T">Type of the Property</typeparam>
        /// <param name="obj">Object from where the Property Value is returned</param>
        /// <param name="propName">Propertyname as string.</param>
        /// <returns>PropertyValue</returns>
        public static T GetPrivatePropertyValue<T>(this object obj, string propName)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            PropertyInfo pi = obj.GetType().GetProperty(propName,
                                                        BindingFlags.Public | BindingFlags.NonPublic |
                                                        BindingFlags.Instance);
            if (pi == null)
                throw new ArgumentOutOfRangeException("propName",
                                                      string.Format("Property {0} was not found in Type {1}", propName,
                                                                    obj.GetType().FullName));
            return (T)pi.GetValue(obj, null);
        }

        /// <summary>
        /// Returns a private Field Value from a given Object. Uses Reflection.
        /// Throws a ArgumentOutOfRangeException if the Property is not found.
        /// </summary>
        /// <typeparam name="T">Type of the Field</typeparam>
        /// <param name="obj">Object from where the Field Value is returned</param>
        /// <param name="propName">Field Name as string.</param>
        /// <returns>FieldValue</returns>
        public static T GetPrivateFieldValue<T>(this object obj, string propName)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            Type t = obj.GetType();
            FieldInfo fi = null;
            while (fi == null && t != null)
            {
                fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                t = t.BaseType;
            }
            if (fi == null)
                throw new ArgumentOutOfRangeException("propName",
                                                      string.Format("Field {0} was not found in Type {1}", propName,
                                                                    obj.GetType().FullName));
            return (T)fi.GetValue(obj);
        }

        /// <summary>
        /// Sets a _private_ Property Value from a given Object. Uses Reflection.
        /// Throws a ArgumentOutOfRangeException if the Property is not found.
        /// </summary>
        /// <typeparam name="T">Type of the Property</typeparam>
        /// <param name="obj">Object from where the Property Value is set</param>
        /// <param name="propName">Propertyname as string.</param>
        /// <param name="val">Value to set.</param>
        /// <returns>PropertyValue</returns>
        public static void SetPrivatePropertyValue<T>(this object obj, string propName, T val)
        {
            Type t = obj.GetType();
            if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null)
                throw new ArgumentOutOfRangeException("propName",
                                                      string.Format("Property {0} was not found in Type {1}", propName,
                                                                    obj.GetType().FullName));
            t.InvokeMember(propName,
                           BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty |
                           BindingFlags.Instance, null, obj, new object[] { val });
        }


        /// <summary>
        /// Set a private Field Value on a given Object. Uses Reflection.
        /// </summary>
        /// <typeparam name="T">Type of the Field</typeparam>
        /// <param name="obj">Object from where the Property Value is returned</param>
        /// <param name="propName">Field name as string.</param>
        /// <param name="val">the value to set</param>
        /// <exception cref="ArgumentOutOfRangeException">if the Property is not found</exception>
        public static void SetPrivateFieldValue<T>(this object obj, string propName, T val)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            Type t = obj.GetType();
            FieldInfo fi = null;
            while (fi == null && t != null)
            {
                fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                t = t.BaseType;
            }
            if (fi == null)
                throw new ArgumentOutOfRangeException("propName",
                                                      string.Format("Field {0} was not found in Type {1}", propName,
                                                                    obj.GetType().FullName));
            fi.SetValue(obj, val);
        }
    }
}

 

Tags
Calendar
Asked by
thorsten wand
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
arman
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Share this question
or