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

How to Preserve the Groups Expand State When New Item is Added

Environment

Product Version 2019.1.220
Product RadTimeline for WPF

Description

How to preserve groups' collapsed/expanded state when new items are added to the RadTimeline.

Solution

Save the names of the TimelineItemGroupControl elements.

public partial class MainWindow : Window 
{ 
    private ObservableCollection<MyPlotInfo> source;         
    private List<string> expandedGroupNames = new List<string>(); 
 
    public MainWindow() 
    { 
        InitializeComponent();             
        this.source = new ObservableCollection<MyPlotInfo>(); 
    }         
 
    private void AddNewDataItem(DateTime start, TimeSpan duration, string groupName) 
    { 
        this.PreserveExpandedGroups(); 
        this.source.Add(new MyPlotInfo() 
        { 
            Start = start, 
            Duration = duration, 
            GroupName = groupName 
        }); 
        this.RestorePreservedGroups(); 
    } 
 
    private void PreserveExpandedGroups() 
    { 
        this.expandedGroupNames.Clear(); 
 
        foreach (var group in this.timeline.GroupedDataItems) 
        {                 
            var groupContainer = this.timeline.GetGroupByGroupHeader(group.GroupKey); 
            if (groupContainer != null && groupContainer.IsExpanded) 
            { 
                this.expandedGroupNames.Add(group.GroupKey); 
            } 
        } 
    } 
 
    private void RestorePreservedGroups() 
    { 
        foreach (var groupName in this.expandedGroupNames) 
        { 
            var groupContainer = this.timeline.GetGroupByGroupHeader(groupName); 
            if (groupContainer != null) 
            { 
                groupContainer.IsExpanded = true; 
            }                 
        } 
    } 
} 
Note that the code snippet is not fully functional. You will need to add a logic that populates the source and also that calls the AddNewDataItem method.

See Also

In this article