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

Toggle LoadOnDemand based on Context.Load

2 Answers 19 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
CD
Top achievements
Rank 1
CD asked on 15 Mar 2011, 07:06 PM
Hello
I am working with your sample here Telerik treeview sample .

I would like to set  IsLoadOnDemandEnabled to false if there are no items returned in the context query.

Currently I am doing this, which appears to work:

private void radTreeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
    // get the clicked Item
    RadTreeViewItem clickedItem = e.OriginalSource as RadTreeViewItem;
    if (clickedItem.Item as Customer != null)
    {
        //_Context.Load(_Context.GetOrdersByCustomerIDQuery((clickedItem.Item as Customer).CustomerID));
        var loadTree = _Context.Load(_Context.GetOrdersByCustomerIDQuery((clickedItem.Item as Customer).CustomerID));
        loadTree.Completed += (s, ev) =>
        {
            //evaluate and disable load on demand?
            if (clickedItem.IsLoadingOnDemand)
            {
                clickedItem.IsLoadOnDemandEnabled = false;
            }
        };
    }
    else if (clickedItem.Item as Order != null)
    {
        _Context.Load(_Context.GetOrderDetailsQuery((clickedItem.Item as Order).OrderID));
    }
}

I think it would make more sense if I could test the results of the Context.Load query, but I don't know how to do this.

Thanks.

2 Answers, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 18 Mar 2011, 05:09 PM
Hello CD,

It is better to change the ExpanderStyle to be blank instead of setting IsLoadOnDemandEnabled to false.
The IsLoadOnDemandEnabled should be used to indicate whether an item could import its child items collection in a future moment when needed. These items could also be imported in the database in a future moment.
However, you can do like so:
private void myTreeView_LoadOnDemand(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            // get the treeview
            RadTreeView tree = sender as RadTreeView;
            // get the clicked Item
            RadTreeViewItem clickedItem = e.OriginalSource as RadTreeViewItem;
  
            if (clickedItem.Item as Customer != null)
            {
              var operation = context.Load(context.GetCustomerOrdersQuery((clickedItem.Item as Customer).CustomerID));
              operation.Completed += (ss, ee) =>
                  {
                      if ((ss as System.ServiceModel.DomainServices.Client.LoadOperation<TreeViewLoadonDemandRIAServices.Web.Order>).Entities.Count() == 0)
                      {
                          clickedItem.ChildrenOfType<ToggleButton>().FirstOrDefault().Template = this.LayoutRoot.Resources["NoExpanderTemplate"] as ControlTemplate;
                      }
                  };
            }
Where the template is defined like so:
<ControlTemplate TargetType="ToggleButton" x:Key="NoExpanderTemplate">
           <Grid />
       </ControlTemplate>
Feel free to ask if you need more info.

Kind regards,
Petar Mladenov
the Telerik team
0
CD
Top achievements
Rank 1
answered on 18 Mar 2011, 09:47 PM
Thanks Petar.
I am not familiar with Linq, so your example with the Entities.Count() method got me pointed in the right direction.
Tags
TreeView
Asked by
CD
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
CD
Top achievements
Rank 1
Share this question
or