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

MVVM Support

The Telerik RadTimeBar control can be used with great success with the Model-View-ViewModel (MVVM) pattern. This help topic will demonstrate how to use the control with the pattern.

  1. Create new ViewModel class that inherits the ViewModelBase abstract class:

        public class ExampleViewModel : ViewModelBase 
            { 
                private DateTime periodStart; 
                private DateTime periodEnd; 
                private IEnumerable<double> data; 
                private DateTime visiblePeriodStart; 
                private DateTime visiblePeriodEnd; 
     
                [TypeConverter(typeof(DateTimeTypeConverter))] 
                public DateTime PeriodStart 
                { 
                    get 
                    { 
                        return this.periodStart; 
                    } 
                    set 
                    { 
                        if (this.periodStart == value) 
                            return; 
     
                        this.periodStart = value; 
                        this.OnPropertyChanged("PeriodStart"); 
                    } 
                } 
     
                [TypeConverter(typeof(DateTimeTypeConverter))] 
                public DateTime PeriodEnd 
                { 
                    get 
                    { 
                        return this.periodEnd; 
                    } 
                    set 
                    { 
                        if (this.periodEnd == value) 
                            return; 
     
                        this.periodEnd = value; 
                        this.OnPropertyChanged("PeriodEnd"); 
                    } 
                } 
     
                [TypeConverter(typeof(DateTimeTypeConverter))] 
                public DateTime VisiblePeriodStart 
                { 
                    get 
                    { 
                        return this.visiblePeriodStart; 
                    } 
                    set 
                    { 
                        if (this.visiblePeriodStart == value) 
                            return; 
     
                        this.visiblePeriodStart = value; 
                        this.OnPropertyChanged("VisiblePeriodStart"); 
                    } 
                } 
     
                [TypeConverter(typeof(DateTimeTypeConverter))] 
                public DateTime VisiblePeriodEnd 
                { 
                    get 
                    { 
                        return this.visiblePeriodEnd; 
                    } 
                    set 
                    { 
                        if (this.visiblePeriodEnd == value) 
                            return; 
     
                        this.visiblePeriodEnd = value; 
                        this.OnPropertyChanged("VisiblePeriodEnd"); 
                    } 
                } 
     
                public IEnumerable<double> Data 
                { 
                    get 
                    { 
                        if (data == null) 
                        { 
                            Random r = new Random(); 
                            List<double> collection = new List<double>(); 
                            for (DateTime date = PeriodStart; date <= PeriodEnd; date = date.AddDays(1)) 
                            { 
                                collection.Add(r.Next(0, 100)); 
                            } 
                            this.data = collection; 
                        } 
                        return this.data; 
                    } 
                } 
            } 

    The PeriodStart and PeriodEnd properties will specify the time period that the TimeBar will visualize. The VisiblePeriodStart and VisiblePeriodEnd properties will specify the visible time period on the screen. The Data collection will be used as datasource for the SparkLines that will be seen inside the TimeBar control.

  2. Add new RadTimeBar and RadSparkLine declarations in XAML and bind the mentioned properties:

    The properties should be bound using TwoWay binding. This is required because of the coercing of the values that happens when a binding is executed.

        <UserControl.DataContext> 
                <local:ExampleViewModel PeriodStart="01/01/2012" PeriodEnd="01/01/2013" VisiblePeriodStart="01/01/2012" VisiblePeriodEnd="01/01/2013" /> 
            </UserControl.DataContext> 
     
            <Grid x:Name="LayoutRoot" Background="White"> 
                <telerik:RadTimeBar Margin="8,8,8,0" Height="150" VerticalAlignment="Top" 
                                    PeriodStart="{Binding PeriodStart, Mode=TwoWay}" 
                                    PeriodEnd="{Binding PeriodEnd, Mode=TwoWay}" 
                                    VisiblePeriodStart="{Binding VisiblePeriodStart, Mode=TwoWay}" 
                                    VisiblePeriodEnd="{Binding VisiblePeriodEnd, Mode=TwoWay}"> 
                    <telerik:RadTimeBar.Intervals> 
                        <telerik:YearInterval/> 
                        <telerik:QuarterInterval/> 
                        <telerik:MonthInterval/> 
                        <telerik:WeekInterval /> 
                        <telerik:DayInterval /> 
                    </telerik:RadTimeBar.Intervals> 
     
                    <telerik:RadLinearSparkline ItemsSource="{Binding Data}" /> 
                </telerik:RadTimeBar> 
            </Grid> 

The result can be seen below:

WPF RadTimeBar with MVVM Data

In this article