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

Asynchronous data loading example without ria services

6 Answers 162 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Rami Abughazaleh
Top achievements
Rank 1
Rami Abughazaleh asked on 25 Sep 2010, 04:33 AM
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

6 Answers, 1 is accepted

Sort by
0
Rami Abughazaleh
Top achievements
Rank 1
answered on 25 Sep 2010, 08:48 PM
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>
0
Rami Abughazaleh
Top achievements
Rank 1
answered on 25 Sep 2010, 09:45 PM
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.
0
Mike
Top achievements
Rank 1
answered on 25 Sep 2010, 10:27 PM
Even with the latest internal build (RadControls_for_Silverlight_4_2010_2_0917), the RowIsExpandedChanged event is not fired.
0
Mike
Top achievements
Rank 1
answered on 25 Sep 2010, 10:40 PM
Ok, with the latest internal build (RadControls_for_Silverlight_4_2010_2_0917), the RowIsExpandedChanging event gets fired. ;)
0
Rami Abughazaleh
Top achievements
Rank 1
answered on 26 Sep 2010, 12:17 AM
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");
}
}
0
Accepted
Yavor Georgiev
Telerik team
answered on 26 Sep 2010, 02:22 AM
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
Tags
TreeListView
Asked by
Rami Abughazaleh
Top achievements
Rank 1
Answers by
Rami Abughazaleh
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Share this question
or