Changing datacontext at runtime

8 posts, 1 answers
  1. Alexandre
    Alexandre avatar
    8 posts
    Member since:
    Mar 2011

    Posted 08 Jul 2011 Link to this post

    Hello!

    I have a self referenced tree view (implementation found somewhere nearby)

    <telerikNavigation:RadTreeView x:Name="treeView" ItemsSource="{Binding Converter={StaticResource HierarchyConverter} }" ItemTemplate="{StaticResource ItemTemplate}" SelectedValuePath="Name" Selected="treeView_Selected">
    </telerikNavigation:RadTreeView>

    and a button with a simple event body

    private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
            {
                var d = (DataItemCollection) treeView.DataContext;
                 
                d.Add(new Period() { Id = 3, ParentId = 1, Name = "Duck1" });
                         
            }

    DataContext changes but there aren't any changes in view. DataItemCollection implements ObservableCollection etc.(also found on the forum). I think i need a two way binding for ItemsSource property but i can't figure out how to achieve this goal. Can you help me?
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 13 Jul 2011 Link to this post

    Hi Alexandre,

    This newly added item cannot activate the converters that build your hierarchy levels again. You have to re-bind when changing the source collection like so:
    private void Button_Click(object sender, RoutedEventArgs e)
           {
               var d = (DataItemCollection)radTreeView.DataContext;
               d.Add(new DataItem() { Id = 15, ParentId = 3, Text = "newly Added Item" });
     
               this.radTreeView.ItemsSource = null;
               this.radTreeView.DataContext = d;
               Binding binding = new Binding();
               binding.Converter = new HierarchyConverter();
               this.radTreeView.SetBinding(RadTreeView.ItemsSourceProperty, binding);
           }
    Please let us know if you need further assistance.

    Regards,
    Petar Mladenov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft banner
  4. Alexandre
    Alexandre avatar
    8 posts
    Member since:
    Mar 2011

    Posted 14 Jul 2011 Link to this post

    Thank you for your assistance. But other problem has appeared. If i rebind treeView it collapses. Trying to expand the parent item that was added a new one does nothing.

    var itemToBeExpanded = ((DataItemCollection)treeView.DataContext).Single(x => x.Id == newOne.ParentId);
     
    treeView.ExpandItemByPath(itemToBeExpanded.Name);

    Any suggestions?
  5. Alexandre
    Alexandre avatar
    8 posts
    Member since:
    Mar 2011

    Posted 18 Jul 2011 Link to this post

    So as i understand there is no way to achieve lazy load in the binded self referenced tree view ?
  6. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 19 Jul 2011 Link to this post

    Hi Alexandre,

    You will need to use ViewModels that will help you implement the Lazy Load technique. You can get the basic idea from this blog post. In your case, the ViewModels will wrap the different tree levels and extract them when needed from the (source collection ) / db table via context. Let us know if this helped you.

    Best wishes,
    Petar Mladenov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  7. Alexandre
    Alexandre avatar
    8 posts
    Member since:
    Mar 2011

    Posted 20 Jul 2011 Link to this post

    void GetEntitiesByParentCompleted(object sender, GetHierarchyEntitiesByParentCompletedEventArgs e)
            {
                var collection = e.Result.ToObservable();
      
                if(treeView.DataContext == null) // setting treeview data at the first time
                {
                    treeView.DataContext = collection;
                }
                else
                {
                    //adding new values to the treeview context
              var currentContext = (DataItemCollection<HierarchyEntity>) treeView.DataContext;
                    currentContext.AddRange(collection);
      
              //rebind the treeview to show newly added values
                    var newBinding = new Binding { Source = treeView.DataContext, Converter = new HierarchyConverter() };
                    treeView.SetBinding(RadTreeView.ItemsSourceProperty, newBinding);
                }
      
                treeView.UpdateLayout();
                  
           var allItems = treeView.ChildrenOfType<RadTreeViewItem>();
      
           // other code...
            }
  8. Alexandre
    Alexandre avatar
    8 posts
    Member since:
    Mar 2011

    Posted 20 Jul 2011 Link to this post

    In the above code i use ChildrenOfType to get all radtreeview items in the tree. But each time it returns different results.

    For example first time it returns 5 items. Then i drill down to the next level and it gives me 5 + 3 newly added items(again for example). Thats correct. But if i go to the third level it again gives me 5 top level items but it should 5 + 3 + ...

    Also i use UpdateLayout method to update treeview after rebinding its data source(in other case ChildrenOfType 
    always returns 0)

    what am i doing wrong?

  9. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 25 Jul 2011 Link to this post

    Hi Alexandre,

     Why do you need the ChildrenOfType<> method? Could you please send us the full code from the method 
    GetEntitiesByParentCompleted ? Please note that the RadTreeViewItems are generated when they are needed - when they are root level Items or their parent is Expanded. So when you re-bind, the previously generated items will be collected by the GarbageCollector and the new ones will be generated when needed. Please elaborate more on your scenario so that we will be better able to assist you. Thank you again for your cooperation.

    Kind regards,
    Petar Mladenov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top
DevCraft banner