LightSwitch Treeview on demand loading with direct OData connection – part 3
Introduction
First read my previous posts on treeviews with on demand loading in LightSwitch.
In this post I present a slight variation: we retrieve the root data not via binding to the viewmodel of the hosting LightSwitch screen, but rather via a direct OData connection.
How?
Our xaml stays pretty the same, except that there is no binding code :
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" x:Class="SilverlightClassLibrary.TelerikTreeWithOdataBinding"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<telerik:HierarchicalDataTemplate x:Name="ItemTemplate">
<TextBlock Text="{Binding DepartmentName}" />
</telerik:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<telerik:RadTreeView x:Name="MyRadTreeView" IsLoadOnDemandEnabled="True" IsExpandOnSingleClickEnabled="True"
ItemPrepared="MyRadTreeView_ItemPrepared" LoadOnDemand="MyRadTreeView_LoadOnDemand"
ItemTemplate="{StaticResource ItemTemplate}" >
</telerik:RadTreeView>
</Grid>
</UserControl>
Since we retrieve the data now via the Odata service, we need to setup a connection to this service and store the root elements in a DataServiceCollection:
using SilverlightClassLibrary.DepartmentDataServiceReference;
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
namespace SilverlightClassLibrary
{
public partial class TelerikTreeWithOdataBinding : UserControl
{
private DepartmentDataServiceReference.DepartmentServiceData _service;
public TelerikTreeWithOdataBinding()
{
InitializeComponent();
Uri uri = new Uri(Application.Current.Host.Source, "../DepartmentServiceData.svc");
_service =
new DepartmentDataServiceReference.DepartmentServiceData(uri);
var query = _service.DepartmentDTOes.Where(d => d.ParentID == null);
var coll = new DataServiceCollection<DepartmentDTO>();
coll.LoadAsync(query);
MyRadTreeView.ItemsSource = coll;
}
private void MyRadTreeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
RadTreeViewItem clickedItem = e.OriginalSource as RadTreeViewItem;
DepartmentDTO dataItem = clickedItem.Item as DepartmentDTO;
var query = _service.DepartmentDTOes.Where(d => d.ParentDepartment.ID == dataItem.ID);
var childCollection = new DataServiceCollection<DepartmentDTO>();
childCollection.LoadCompleted += (s, e2) =>
{
clickedItem.IsLoadOnDemandEnabled = true;
clickedItem.ItemsSource = childCollection;
clickedItem.IsExpanded = true;
clickedItem.IsLoadingOnDemand = false;
};
childCollection.LoadAsync(query);
}
private void MyRadTreeView_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
{
var treeViewItem = e.PreparedItem;
var dataItem = treeViewItem.Item as DepartmentDTO;
if (dataItem.ChildrenCount <= 0)
{
treeViewItem.IsLoadOnDemandEnabled = false;
}
else
{
treeViewItem.IsLoadOnDemandEnabled = true;
}
}
}
}
The most striking difference is that our user control has no reference at all to LightSwitch Dlls !
So, you could perfectly port the above code to another non-LightSwitch silverlight application.
Conclusion
Another way of retrieving the data. For me, it’s not yet clear which approach is the best: this one or via the LightSwitch binding.
In a next post (but not for today) I will introduce searching and filtering in the treeview control.
Enjoy !


[...] Van Bladel (@paulbladel) continued his series with LightSwitch Treeview on demand loading with direct OData connection – part 3 on [...]
[...] LightSwitch Treeview on demand loading with direct OData connection – part 3 [...]
[...] LightSwitch Treeview on demand loading with direct OData connection – part 3 [...]