New to Telerik UI for WinUI? Download free 30-day trial

Formatter Provider

This help topic will give an overview on the Formatter Provider feature of RadTimeBar.

Using Formatter Providers

RadTimeBar provides means for customizing the interval formats through the FormatterProvider property of the IntervalBase class. Using it, you can specify different formats for each interval. To define a formatter provider, create a class that implements the IIntervalFormatterProvider interface.

The IIntervalFormatterProvider interface

A formatter provider is a class that implements the IIntervalFormatterProvider interface. This interface has two methods that need to be implemented - GetFormatters and GetIntervalSpanFormatters. They both have to return a Func[] - an array of functions each of which given a DateTime value returns it as formatted string.

  • GetFormatters—Used in the default scenario when Interval.CurrentIntervalSpan = 1, i.e. there is a period control for every interval item.
  • GetIntervalSpanFormatters—Used when one period control corresponds to a couple of interval items. E.g. an HourInterval with CurrentIntervalSpan = 12 interval span will create two hour intervals per each day - (0:00 - 12:00) and (12:00 - 0:00). An appropriate format would be String.Format("{0} - {1}", currentIntevalString, nextIntervalString).

The following sample shows an implementation of a custom hour formatter provider and how to set it:

Creating a custom formatter provider

public class HourFormatterProvider : IIntervalFormatterProvider { private static Func[] formatters; private Func[] intervalSpanFormatters;

    static HourFormatterProvider() 
    { 
        formatters = new Func<DateTime, string>[] 
        { 
            date => date.ToString("H:mm"), 
            date => date.ToString("HH") 
        }; 
    } 
 
    public Func<DateTime, string>[] GetFormatters(IntervalBase interval) 
    { 
        return formatters; 
    } 
 
    public Func<DateTime, string>[] GetIntervalSpanFormatters(IntervalBase interval) 
    { 
        if (this.intervalSpanFormatters == null) 
        { 
            this.intervalSpanFormatters = new Func<DateTime, string>[] 
            { 
                date => String.Format("{0} - {1}", date.ToString("dddd H:mm"), interval.IncrementByCurrentInterval(date).ToString("H:mm")) 
            }; 
        } 
 
        return this.intervalSpanFormatters; 
    } 
} 

Setting the custom formatter provider

<telerik:RadTimeBar x:Name="timeBar"> 
    <telerik:RadTimeBar.Resources> 
        <local:HourFormatterProvider x:Key="HourFormatterProvider"/> 
    </telerik:RadTimeBar.Resources> 
    <telerik:RadTimeBar.Intervals> 
        <telerikDataVisualization:HourInterval FormatterProvider="{StaticResource HourFormatterProvider}"/> 
    </telerik:RadTimeBar.Intervals> 
</telerik:RadTimeBar> 
RadTimeBar with a custom formatter provider for the HourInterval

RadTimeBar with a custom formatter provider for the HourInterval

In this article
Not finding the help you need?