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

Week Numbers

1 Answer 37 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
EmpowerIT
Top achievements
Rank 2
EmpowerIT asked on 29 Aug 2011, 01:36 PM
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

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 01 Sep 2011, 04:06 PM
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 >>

Tags
Calendar
Asked by
EmpowerIT
Top achievements
Rank 2
Answers by
Victor
Telerik team
Share this question
or