Toggle LoadOnDemand based on Context.Load

3 posts, 1 answers
  1. CD
    CD avatar
    14 posts
    Member since:
    Mar 2011

    Posted 15 Mar 2011 Link to this post

    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. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 Mar 2011 Link to this post

    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
  3. DevCraft banner
  4. CD
    CD avatar
    14 posts
    Member since:
    Mar 2011

    Posted 18 Mar 2011 Link to this post

    Thanks Petar.
    I am not familiar with Linq, so your example with the Entities.Count() method got me pointed in the right direction.
Back to Top