MVVM support
The RadTimeline control can be used with great success with the Model-View-ViewModel (MVVM) pattern. This help topic will demonstrate how to bind the control with the pattern.
-
We'll get started with a class with two properties - Duration and Date. They will be used to represent the two types of items in the Timeline. We'll also need a method that will set values to the newly created properties and will return a collection of our business objects.
C#public class Product { public TimeSpan Duration { get; set; } public DateTime Date { get; set; } public static ObservableCollection<Product> GetData(int count) { var startDate = new DateTime(2010, 1, 1); var endDate = new DateTime(2012, 2, 1); Random r = new Random(); ObservableCollection<Product> result = new ObservableCollection<Product>(); for (DateTime i = startDate; i < endDate; i = i.AddMonths(1)) { result.Add(new Product() { Date = i, Duration = TimeSpan.FromDays(r.Next(50, 100))} ); } for (int i = 0; i < 15; i++) { result.Add(new Product() { Date = startDate.AddMonths(r.Next(0, 25)).AddDays(15) }); } return result; } } -
Create new class that inherits the ViewModelBase abstract class. This will be your ViewModel. What we'll need to add in it - an ObservableCollection that will be used as data source. In the constructor of the class call the GetData method we created in our Product class. (check Step 1.) The parameter of the method will represent the number of items in the timeline.
ViewModelBase class is part of the Telerik.Windows.Controls.dll
C#public class ExampleViewModel : ViewModelBase { private ObservableCollection<Product> _data; public ObservableCollection<Product> Data { get { return this._data; } set { if (this._data != value) { this._data = value; this.OnPropertyChanged("Data"); } } } public ExampleViewModel() { this.Data = Product.GetData(15); } } -
Add new RadTimeline declarations in XAML and bind the StartPath and DurationPath properties. To learn more about these properties, please check the DataBinding topic:
XAML<telerik:RadTimeline Height="250" x:Name="RadTimeline1" VerticalAlignment="Top" Margin="6" PeriodStart="2011/01/01" PeriodEnd="2011/06/01" StartPath="Date" DurationPath="Duration" ItemsSource="{Binding Data}"> <telerik:RadTimeline.Intervals> <telerik:YearInterval /> <telerik:MonthInterval /> <telerik:WeekInterval /> <telerik:DayInterval /> </telerik:RadTimeline.Intervals> </telerik:RadTimeline>