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

Changing datacontext at runtime

7 Answers 141 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alexandre
Top achievements
Rank 1
Alexandre asked on 08 Jul 2011, 02:13 PM
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?

7 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 13 Jul 2011, 02:55 PM
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!

0
Alexandre
Top achievements
Rank 1
answered on 14 Jul 2011, 07:11 AM
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?
0
Alexandre
Top achievements
Rank 1
answered on 18 Jul 2011, 10:25 AM
So as i understand there is no way to achieve lazy load in the binded self referenced tree view ?
0
Petar Mladenov
Telerik team
answered on 19 Jul 2011, 01:10 PM
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!

0
Alexandre
Top achievements
Rank 1
answered on 20 Jul 2011, 10:38 AM
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...
        }
0
Alexandre
Top achievements
Rank 1
answered on 20 Jul 2011, 10:50 AM
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?

0
Accepted
Petar Mladenov
Telerik team
answered on 25 Jul 2011, 01:13 PM
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!

Tags
TreeView
Asked by
Alexandre
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Alexandre
Top achievements
Rank 1
Share this question
or