Getting CheckedItems without opening all the nodes

4 posts, 0 answers
  1. Paolo
    Paolo avatar
    423 posts
    Member since:
    Jun 2009

    Posted 09 Sep 2010 Link to this post

    Hello,
    I've got a 5 level nested treeview,  the last level got the ID i need to process...
    When the treeview loads (it has a lot of items, almost 15.000) it's collapsed and the user can choose to drilldown or to check the root level and generate a report clicking on a button....

    The problem is I won't get the CheckedItems of the leaf till I open the previous 4 level.... its' not good since I can't ask the user to opeen each n-tier level and neither I can start it expanded... I've tried setting IsVirtualized = false but got no luck

    Thanks

    Paolo
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 14 Sep 2010 Link to this post

    Hello Paolo,

    The problem with such scenarios is that due to the large number of items, their containers (RadTreeViewItem) will not be created. That is why the check logic has to be implemented within the ViewModel itself i.e. the ViewModel has to keep a collection of all the checked items. I've prepared a sample project that demonstrates one possible workaround to this scenario.
    In it, my ViewModel has 2 properties - CheckedItems (a collection that contains all the checked items) and DataItems (a collection with all the items). The CheckedItems and DataItems collections are of type ObservableCollection<DataItem>. DataItem is a hierarchical object - it has 4 properties - Name, ViewModel, CheckState and Children.
    Name is just a string representation for the item. Children is a collection of type ObservableCollection<DataItem> and contains all the sub-items.
    CheckState is a property of DataItem that is bound to the CheckState of RadTreeViewItem via ContainerBindings. When CheckState in DataItem is set, I use the ViewModel property to add/remove the current DataItem from the CheckedItems collection of the ViewModel:

    public ToggleState CheckState
    {
        get
        {
            return this.checkState;
        }
        set
        {
            if (this.checkState != value)
            {
                this.checkState = value;
     
                // Raise property changed
                this.OnPropertyChanged("CheckState");
     
                // If the DataItem is checked
                //  1. add it to the CheckedItems collection
                //  2. check all its sub-items
                if (this.checkState == ToggleState.On)
                {
                    this.ViewModel.CheckedItems.Add(this);
     
                    foreach (var item in this.Children)
                    {
                        item.CheckState = ToggleState.On;
                    }
                }
     
                // If the DataItem is unchecked
                //  1. remove it from the CheckedItems collection
                //  2. uncheck all its sub-items
                if (this.checkState == ToggleState.Off)
                {
                    this.ViewModel.CheckedItems.Remove(this);
     
                    foreach (var item in this.Children)
                    {
                        item.CheckState = ToggleState.Off;
                    }
                }
            }
        }
    }

    Basically, the rule of thumb in such, scenarios where the hierarchy is deep, is to maintain the checked items in the ViewModel and not rely on the containers (RadTreeViewItems) since they might not be created. Imagine all the containers were created for your particular scenario - 15.000 items. It would be a performance hell IMHO.

    Let me know if you have additional questions or comments regarding this scenario.

    Kind regards,
    Kiril Stanoev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Paolo
    Paolo avatar
    423 posts
    Member since:
    Jun 2009

    Posted 14 Sep 2010 Link to this post

    Hello Kiril,
    do you think I can implement this in thew WebServer then pass it to SL or have I to create this object when the call for getting the data has been done?
    Thanks
    Paolo
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 16 Sep 2010 Link to this post

    Hello Paolo,

    After you receive the data from the web server, you have to create the objects(ViewModel) on the Silverlight client. More on WCF Ria services you can find here.

    All the best,
    Kiril Stanoev
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
Back to Top