LightSwitch Treeview on demand loading with direct OData connection – part 3


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.



Our xaml stays pretty the same, except that there is no binding code :

    xmlns:telerik="" x:Class="SilverlightClassLibrary.TelerikTreeWithOdataBinding"
    d:DesignHeight="300" d:DesignWidth="400">

        <telerik:HierarchicalDataTemplate x:Name="ItemTemplate">
            <TextBlock Text="{Binding DepartmentName}" />
    <Grid x:Name="LayoutRoot" Background="White">
        <telerik:RadTreeView x:Name="MyRadTreeView" IsLoadOnDemandEnabled="True"  IsExpandOnSingleClickEnabled="True"
                             ItemPrepared="MyRadTreeView_ItemPrepared" LoadOnDemand="MyRadTreeView_LoadOnDemand"
                             ItemTemplate="{StaticResource ItemTemplate}" >


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()
            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>();
            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;

        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;
                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.


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 !