How to Bind Dictionary to RadTimeline

4 posts, 0 answers
  1. Manuel
    Manuel avatar
    6 posts
    Member since:
    May 2015

    Posted 08 Sep 2015 Link to this post

    Hi, 

     

    How can I bind a Dictionary object to a Timeline.

     

    For example:

     MyMainClass with the Attribute Dictionary

        Dictionary <string,MycustomObject> myObject = new Dictionary <string,MycustomObject>();

     

    MycustomObject

    Dictionary<int,AnotherObject> myOtherDictionary = new Dictionary<int,ANotherObject> ();

    Date StartDate

    Date EndDate

     

    Class AnotherObject

     

    Date DateItem

    int DurationItem

     

    I tried to asign to the ItemSource the dictionary in this way  but it doesn't work 

     

    ItemSource="{Binding Path=myObject}"

    StartDate="{Binding StartDate}"

    DurationPath=DurationItem

    Thanx's in advance

  2. Peshito
    Admin
    Peshito avatar
    497 posts

    Posted 11 Sep 2015 Link to this post

    Hello Manuel,

    You can data bind the Timeline with a Dictionary by using its Key and Value parameters. For instance:
    <telerik:RadTimeline Height="250"
                   VerticalAlignment="Top"
                   Margin="6"
                   PeriodStart="{Binding StartDate, Mode=TwoWay}"
                   PeriodEnd="{Binding EndDate, Mode=TwoWay}"
                   StartPath="Value"
                   DurationPath="Key"
                   ItemsSource="{Binding Data}">
           <telerik:RadTimeline.Intervals>
               <telerik:YearInterval />
               <telerik:MonthInterval />
               <telerik:WeekInterval />
               <telerik:DayInterval />
           </telerik:RadTimeline.Intervals>
       </telerik:RadTimeline>
    Attached is a sample project demonstrating this approach.

    Hope it helps.

    Regards,
    Peshito
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Manuel
    Manuel avatar
    6 posts
    Member since:
    May 2015

    Posted 12 Sep 2015 in reply to Peshito Link to this post

    Hi Peshito



    Thank you for your answer. It works.

     But what if the object that is assigned to the datacontext is not an "object" but a Dictionary.

     
    I mean in the example that you attach  is posible not to assign the product object  but  assign a Dictionary to the context  like

     

    Dictionary<string, Product> myDictionary = new Dictionary<string, Product>();

    myDictionary.Add("1",new Product () { StartDate=dateStart,EndDate:endDate,Data=items});

    myDictionary.Add("2",new Product () { StartDate=dateStart,EndDate:endDate,Data=items});

     (obviously Keeping the structure of product object )

    Is this possible ?


  5. Peshito
    Admin
    Peshito avatar
    497 posts

    Posted 16 Sep 2015 Link to this post

    Hello Manuel,

    If I understood you correctly what you would like to achieve is to set a different datacontext depending on the key value of your Dictionary. If this is so, changing the MainWindow() constructor like shown below should do the job:
    public MainWindow()
    {
        InitializeComponent();
         
        var startDate = DateTime.Today;
        var endDate = DateTime.Today.AddYears(1);
     
     
        var items = new Dictionary<TimeSpan, DateTime>();
        for (int i = 1; i <= 10; i++)
        {
            items.Add(TimeSpan.FromDays(10 + i * 5), DateTime.Today.AddMonths(i));
        }
     
        //this.DataContext = new Product() { StartDate = startDate, EndDate = endDate, Data = items };
     
        Dictionary<string, Product> myDictionary = new Dictionary<string, Product>();
     
        myDictionary.Add("1", new Product() { StartDate = startDate, EndDate = endDate, Data = items });
     
        myDictionary.Add("2", new Product() { StartDate = startDate, EndDate = endDate, Data = items });
     
        this.timeline.DataContext = myDictionary["1"];
    }
    In case this is not what you meant, please keep in mind that the Timeline control works with a single collection from your ViewModel and you should keep all the data that you want to display in the Timeline in one place.

    Regards,
    Peshito
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top