Week Numbers

2 posts, 0 answers
  1. EmpowerIT
    EmpowerIT avatar
    117 posts
    Member since:
    Jun 2007

    Posted 29 Aug 2011 Link to this post

    Hi Guys,

    Where would be the best place to override the value displayed in week numbers?

    I would like to create my own numbering. Alternate weeks (1,2,1,2,1,2), four week cycles (1,2,3,4,1,2,3,4) etc

    Regards,
    Tarek
  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 01 Sep 2011 Link to this post

    Hi Empowerit,

     Thank you for writing.
    This can be accomplished with a custom template selector and a binding converter.
    The custom template selector will select a special item template that uses a value converter in order to convert
    the regular week numbers to something else.

    Here are the implementations of the template selector and the value converter:

    public class WeekNumberConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int val = int.Parse((string)value);
     
            // Modify result taking into accound the actual value number.
            // This converter helps visualize alternating weeks.
            int result = val % 2;
     
            return (result + 1).ToString();
        }
     
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    }
     
     
    public class CustomTemplateSelector : DataTemplateSelector
    {
        private DataTemplate weekNumberTemplate;
     
        public CustomTemplateSelector(DataTemplate weekNumberTemplate)
        {
            this.weekNumberTemplate = weekNumberTemplate;
        }
     
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            CalendarButton button = (CalendarButton)container;
            if (button.ButtonType == CalendarButtonType.WeekNumber)
            {
                return this.weekNumberTemplate;
            }
     
            return null;
        }
    }

    In order to use these classes you need to declare that custom item template in the page's resources. For example:
    <UserControl.Resources>
        <local:WeekNumberConverter x:Key="MyWeekNumberConverter"/>
        <DataTemplate x:Key="WeekNumberTemplate">
            <Grid Margin="5">
                <TextBlock Text="{Binding DetailText}" FontSize="7" MaxHeight="25" VerticalAlignment="Top" Margin="0,-2,0,0"/>
                <TextBlock Text="{Binding Path=Text, Converter={StaticResource MyWeekNumberConverter}}" x:Name="TextPresenter" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

    Finally you have to modify RadCalendar to use your custom template selector. I assume you have the following calendar declared:
    <telerikInput:RadCalendar x:Name="calendar"
                              WeekNumbersVisibility="Visible"/>

    Now we can tell the calendar to use the new template selector:
    public MainPage()
    {
        InitializeComponent();
        CustomTemplateSelector selector = new CustomTemplateSelector(this.Resources["WeekNumberTemplate"] as DataTemplate);
        this.calendar.ItemTemplateSelector = selector;
    }

    I hope this solutions is helpful.
    Please write again if you need further assistance with our controls.
    Greetings,
    Victor
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
Back to Top