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
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);
}
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!
var itemToBeExpanded = ((DataItemCollection)treeView.DataContext).Single(x => x.Id == newOne.ParentId);
treeView.ExpandItemByPath(itemToBeExpanded.Name);
Any suggestions?
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!
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...
}
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?
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.
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!