Asynchronous data loading example without ria services

7 posts, 1 answers
  1. Rami Abughazaleh
    Rami Abughazaleh avatar
    27 posts
    Member since:
    Feb 2010

    Posted 24 Sep 2010 Link to this post

    Hi.

    Does anyone have a working silverlight client-side example of asynchronously loading data into a RadTreeListView without using ria services?

    I have tried the following:

    DataLoadMode="Asynchronous"
    IsBusy="{Binding IsBusy}"

    <telerik:RadTreeListView.ChildTableDefinitions>
     <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items, Mode=TwoWay}" />
    </telerik:RadTreeListView.ChildTableDefinitions>
    <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" />

    public class PersonViewModel : INotifyPropertyChanged
    {
    public long Idgetset; }
    public string Name { getset; }
    public bool IsBusy = false;

    private bool childrenLoaded = false;
    private ObservableCollection<PersonViewModel> items;

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
    }
    }

    public ObservableCollection<PersonViewModel> Items
    {
    get
    {
    if (!childrenLoaded)
    {
    this.IsBusy = true;
    MyWCFServiceClient client = MyWCFServiceFactory.Create();
    client.GetChildrenCompleted += (s, e) =>
    {
    if (e.Error != null)
    {
    //TODO: Handle error
    return;
    }

    ObservableCollection<Person> children = e.Result;

    this.items = new ObservableCollection<PersonViewModel>();
    foreach (Person child in children) {
    PersonViewModel personViewModel = new PersonViewModel()
    personViewModel.Id = child.Id;
    
    personViewModel.Name = child.Name;
    this.items.Add(personViewModel);
    }

    childrenLoaded = true;
    this.IsBusy = false;
    NotifyPropertyChanged("Items");
    };

    client.GetChildrenAsync(this.Id);
    }
    return items;
    }
    }
    } But the RadTreeListView does not get updated when NotifyPropertyChanged("Items") is called. I expected the IsBusy indicator to appear during loading, but it did not. I expected the expander\collapser icon to appear after NotifyPropertyChanged("Items") is called, but it did not. Any ideas? Thank you. RadControls_for_Silverlight_4_2010_2_0812
  2. Rami Abughazaleh
    Rami Abughazaleh avatar
    27 posts
    Member since:
    Feb 2010

    Posted 25 Sep 2010 Link to this post

    Ok, I am now able to get items to show up hierarchically, but they are not loaded on demand \ lazy binding.
    Instead, all the children are loaded immediately.

    Any ideas?

    Thank you.

    <telerik:HierarchicalDataTemplate x:Key="Item">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" />
    </StackPanel>
    </telerik:HierarchicalDataTemplate>
    <telerik:HierarchicalDataTemplate x:Key="RootItem" />
    <telerik:RadTreeListView DataLoadMode="Asynchronous">
    <telerik:RadTreeListView.ChildTableDefinitions>
    <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" />
    </telerik:RadTreeListView.ChildTableDefinitions>
    <telerik:RadTreeListView.Columns>
    <telerik:GridViewDataColumn Header="Name" CellTemplate="{StaticResource Item}" />
    </telerik:RadTreeListView.Columns>
    </telerik:RadTreeListView>
  3. DevCraft banner
  4. Rami Abughazaleh
    Rami Abughazaleh avatar
    27 posts
    Member since:
    Feb 2010

    Posted 25 Sep 2010 Link to this post

    If I use a RadGridView instead, I get the load on demand effect, but the children do not re-use the columns of the parent which is what I am trying to achieve.
  5. Mike
    Mike avatar
    17 posts
    Member since:
    Jul 2010

    Posted 25 Sep 2010 Link to this post

    Even with the latest internal build (RadControls_for_Silverlight_4_2010_2_0917), the RowIsExpandedChanged event is not fired.
  6. Mike
    Mike avatar
    17 posts
    Member since:
    Jul 2010

    Posted 25 Sep 2010 Link to this post

    Ok, with the latest internal build (RadControls_for_Silverlight_4_2010_2_0917), the RowIsExpandedChanging event gets fired. ;)
  7. Rami Abughazaleh
    Rami Abughazaleh avatar
    27 posts
    Member since:
    Feb 2010

    Posted 25 Sep 2010 Link to this post

    Ok, I am getting closer but my child items do not appear as expected.

    Any ideas?

    <telerik:RadTreeListView 
    x:Name="tlv"
    RowIsExpandedChanging="tlvRestoreFiles_RowIsExpandedChanging" 
    RowLoaded="tlvRestoreFiles_RowLoaded">
    <telerik:RadTreeListView.ChildTableDefinitions>
    <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items, Mode=TwoWay}" />
    </telerik:RadTreeListView.ChildTableDefinitions>
    <telerik:RadTreeListView.Columns>
    <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" MinWidth="150" />
    </telerik:RadTreeListView.Columns>
    </telerik:RadTreeListView>

    private void tlv_RowIsExpandedChanging(object sender, Telerik.Windows.Controls.GridView.RowCancelEventArgs e)
    {
    GridViewRowItem item = e.Row;

    PersonViewModel model = item.DataContext as PersonViewModel;
    if (model.Items == null)
    {
    tlv.IsBusy = true;

    model.Items = new ObservableCollection<PersonViewModel>();

    MyWCFServiceClient client = MyWCFServiceFactory.Create(); 
    client.GetChildrenCompleted += (s, e) =>
    {
    if (e.Error != null)
    { //TODO: Handle error return; }
    ObservableCollection<Person> children = e.Result;
    foreach (Person child in children) {
    PersonViewModel personViewModel = new PersonViewModel()
    personViewModel.Id = child.Id;
    
    personViewModel.Name = child.Name;
    model.Items.Add(personViewModel);
    }
    model.NotifyItemsPropertyChanged();
    tlv.IsBusy = false;
    };
    client.GetChildrenAsync(this.Id); }
    }
    //automatically expand the root item
    private void tlv_RowLoaded(object sender, RowLoadedEventArgs e)
    {
    GridViewRowItem item = e.Row;
    PersonViewModel model = item.DataContext as PersonViewModel;
    if (model != null)
    { if (model.Id == 0) {
    GridViewRow row = item as GridViewRow;
    row.IsExpandable = true;
    row.IsExpanded = true; }
    }
    }

    //load initial data
    PersonViewModel personView = new PersonViewModel();
    personView.Id = 0;
    personView.Name = "Root";

    tlv.ItemsSource = new ObservableCollection<PersonViewModel>(){ personView };


    public class PersonViewModel : INotifyPropertyChanged
    {
    public string Name { getset; }

    public ObservableCollection<PersonViewModel> Items {getset;}

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
    }
    }

    public void NotifyItemsChanged()
    {
    this.NotifyPropertyChanged("Items");
    }
    }
  8. Answer
    Yavor Georgiev
    Admin
    Yavor Georgiev avatar
    982 posts

    Posted 25 Sep 2010 Link to this post

    Hello Rami Abughazaleh,

     For this to work, you shouldn't reset the value of the Items property of your model. You need to set it once, preferably in the constructor of your model, then just add and remove items from the ObservableCollection. Also, make sure you have the Latest Internal Build assemblies, as load-on-demand for RadTreeListView is not avaiable in the 0812 assemblies.

    Best wishes,
    Yavor Georgiev
    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
DevCraft banner