How do I expand row from ViewModel when using Load On Demand?

4 posts, 0 answers
  1. Rob Conley
    Rob Conley avatar
    47 posts
    Member since:
    Sep 2012

    Posted 12 Feb 2013 Link to this post

    When I only have 1 row under certain conditions, I want to expand the row and show second level load on demand items (not 3rd or deeper).

    I have populated my child collection in the ViewModel and have TwoWay binding set, but there must be another trick to getting the TreeListView to expand the row.

    Any suggestions are appreciated.

    -Rob
  2. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 13 Feb 2013 Link to this post

    Hello,

     The current official load on demand demo can be found here. With our upcoming official Q1 2013 release we will introduce two new properties (IsExpandedBinding and IsExpandableBinding) related to better MVVM, expand/collapse and load on demand for RadTreeListView. 

    Here is an example code from our still unreleased demo:

    <Grid x:Name="LayoutRoot">
            <Grid.Resources>
                <telerik:InvertedBooleanConverter x:Key="c" />
            </Grid.Resources>
     <telerik:RadTreeListView ItemsSource="{Binding FoldersOnDemand}"
                                     IsExpandedBinding="{Binding IsExpanded, Mode=TwoWay}"
                                     IsExpandableBinding="{Binding IsEmpty, Converter={StaticResource c}}"
    >
       <telerik:RadTreeListView.ChildTableDefinitions>
           <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" />
        </telerik:RadTreeListView.ChildTableDefinitions>
     </telerik:RadTreeListView>
    </Grid>
    ....
            private bool isExpanded;

            [Display(AutoGenerateField = false)]
            public bool IsExpanded
            {
                get
                {
                    return this.isExpanded;
                }
                set
                {
                    if (this.isExpanded != value)
                    {
                        this.isExpanded = value;

                        this.LoadChildren();

                        OnPropertyChanged("IsExpanded");
                    }
                }
            }

            public void LoadChildren()
            {
                if (this.items == null)
                {
                    this.items = new ObservableCollection<FolderViewModel>(from f in this.folderElement.Elements("folder")
                                                                           select new FolderViewModel(f)
                                                                           {
                                                                               Name = f.Attribute("Name").Value,
                                                                               IsEmpty = bool.Parse(f.Attribute("IsEmpty").Value),
                                                                               CreatedOn = DateTime.Parse(f.Attribute("CreationTime").Value, System.Globalization.CultureInfo.InvariantCulture),
                                                                           });
                    this.OnPropertyChanged("Items");
                }
            }
    ....

    Regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Rob Conley
    Rob Conley avatar
    47 posts
    Member since:
    Sep 2012

    Posted 13 Feb 2013 Link to this post

    Thanks for the quick reply.

    One thing that has confused me in the past examples is the use of the ViewModel class itself as the collection class or using a collection within the view model as the binding object in XAML. I once thought the ViewModel itself had to be a collection.

    It works either way, correct? In other words, IsExpanded can be a property on the view model or on a collection within the ViewModel that the list is bound to in XAML, correct?
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 13 Feb 2013 Link to this post

    Hi,

    The DataContext for both IsExpandedBinding and IsExpandableBinding is the data item associated with every row in the treelist. 

    Regards,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top