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

Redraw timeline without reseting ItemSource

5 Answers 154 Views
TimeLine
This is a migrated thread and some comments may be shown as answers.
Albert
Top achievements
Rank 1
Albert asked on 17 Apr 2015, 07:27 AM

In my project, I have a timeline that is updated every second or so.

At each second, an Item with a duration representing the elapsed time is updated : its duration is modified.

 

 When only the duration of one item changes, the timeline is redrawn when the VisibleStartDate and/or VisibleEndDate are changed.

If the visible zone is not changed, the only way to make the new duration of the item appear on the timeline is to use the Setter on ItemsSource.  But this results in poor performances, most notably because I have implemented a mechanism that remembers the selected items when the Data is refreshed (it reselects it).

 

So in order to only redraw the timeline without having to update the ItemSource, I resolved to this uglyhack (that works, though).

In the view model.

public void UpdateItemDurationAndRefresh()
{
    itemWithDuration.Duration = GetNewTimeSpan();
    this.VisibleDate = this.VisibleDate.AddMilliseconds(-1);
    this.VisibleDate = this.VisibleDate.AddMilliseconds(1);
}

 I tried the following, but the duration of the item is not visible until some other interactions either changes the visible zone, or the ItemSource is actually updated.

public void UpdateItemDurationAndRefresh() // DOES NOT WORK
{
    itemWithDuration.Duration = GetNewTimeSpan();
    this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Data"));
   // "Data" is the name of the collection containing "itemWithDuration",
   // and is binded to the GUI thanks to the XAML code
}

 

 

Is there anyway to redraw the timeline without having to reset the ItemSource, just like what is done when the visible zone is modified ?

 

Thanks

5 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 20 Apr 2015, 07:09 AM
Hello Albert,

Yes, you can update the timeline only by updating the duration of the time. However, in order for this  updated to be reflected on the UI, the PropertyChanged event should be raised with the property that represent's the item's duration. 

Here is an example in code:
public class TimelineDataModel : ViewModelBase
{
    private TimeSpan duration;
    public TimeSpan Duration
    {
        get { return this.duration; }
        set
        {
            if (this.duration != value)
            {
                this.duration = value;
                OnPropertyChanged("Duration");
            }
        }
    }  
}

TimelineDataModel itemWithDuration;
public void UpdateItemDurationAndRefresh()
{
    itemWithDuration.Duration = GetNewTimeSpan();
}

Please try this and let me know if it works for you.

Regards,
Martin
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Albert
Top achievements
Rank 1
answered on 20 Apr 2015, 07:35 AM

Unfortunately no, it does not update the timeline. Just to be clear, I included extracts of my code. The main difference between my project and what you wrote, is that the object I want to modify is part of the  list that is declared as the source of the timeline.

Consequently, the duration itself is not a property of the view model. The code that declares my timeline is similar to this :

 

<telerik:RadTimeline
  ItemsSource="{Binding Data}"
  DurationPath="Duration"
  StartPath="Date"
 >

While the viewmodel that populate the timeline is similar to this :

class TimelineViewModel : ViewModelBase
{
     
    private ITimelineItem itemWithDuration;
     
    private List<ITimelineItem> timelineItems;
    public List<ITimelineItem> Data
    {
        get { return timelineItems; }
        set { timelineItems = value; }
    }
     
    public TimelineViewModel()
    {
        mTimelineItems = new List<ITimelineItem>();
        itemWithDuration = new ItemWithDuration();
    }
     
    public void UpdateViewModel()
    {
        itemWithDuration.Duration = GetNewTimeSpan();
        OnPropertyChanged("Duration");
    }
}

 The UpdateViewModel() methods does not graphically update the duration of the item.

Thanks you for any idea you would be willing to share.

0
Albert
Top achievements
Rank 1
answered on 20 Apr 2015, 07:37 AM
NB: As one would have guessed, the ItemWithDuration is added in the "Data" list. Hence the observation of it not being graphically updated.
0
Accepted
Martin Ivanov
Telerik team
answered on 20 Apr 2015, 02:06 PM
Hi Albert,

Note that since "Duration" is not a property of the TimelineViewModel its OnPropertyChanged() method won't work with the "Duration" that is passed as a string argument. The OnPropertyChanged() should be called in the ItemWithDuration class. I prepared a project based on your code that demonstrates updating of a timeline item's duration at runtime. Please give it a try and let me know if I am missing something.

Regards,
Martin
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Albert
Top achievements
Rank 1
answered on 20 Apr 2015, 02:35 PM

Oh I get it!

I made the ItemWithDuration inherit ViewModelBase, and the notification are now properly propagated !

 

I must say this property change notification mechanism is not totally clear to me at the moment, but I guess that this is beyond the  scope of this thread to further talk about it.

 

Thank you for your time, and the help you provided, I now have a definitely more elegant solution to my problem.

And by the way, good job with the WPF components, they are a pleasure to work with ;)

 

 

 

 

Tags
TimeLine
Asked by
Albert
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Albert
Top achievements
Rank 1
Share this question
or