It appears setting the Expanded property in the ItemDataBound event doesn't work. Is that correct? What options do I have?
For example: Setting item.Expanded doesn't appear to do anything. (I verified IsExpanded is true in my models)
@(Html.Kendo().TreeView()
.Name("TestTree")
.ExpandAll(false)
.BindTo(Model.Items, mappings =>
{
mappings.For<
MyModelClass
>(binding =>
binding.ItemDataBound((item, child) =>
{
item.Text = child.Name;
item.Selected = child.IsSelected;
item.Expanded = child.IsExpanded;
})
.Children(x => x.ChildItems)
);
})
)
6 Answers, 1 is accepted
The ExpandAll(false) call registers a post-processing action that is executed after the call to BindTo. Thus, after binding, all Expanded flags are set to false. To resolve this, remove the call to ExpandAll.
Regards,
Alex Gyoshev
Telerik
Hello,
Removing ExpandAll fixed the issue -- until we upgraded to Q3 2016. Now the binding to Expanded is not working again.
The code above without the call to ExpandAll is not expanding items where item.IsExpanded is true. (I put a breakpoint on the binding method and verified child.IsExpanded was true.)
Attached you will find a simple MVC 5 application, based on your scenario, using the R3 2016 SP1 release. When testing it on our side, the Expanded property of the item is properly mapped from the property of the ViewModel.
May I ask you to specify how to modify this sample so it reproduces the problem observed?
Regards,
Veselin Tsvetanov
Telerik by Progress
Hi Veselin,
Thank you for taking the time to create the project. Our code was setting item.HasChildren = child.ChildItems.Any() in the databinding. Once I added that to your project, I reproduced the issue. However, it looks like HasChildren does not need to be set - I assume it is for load on demand trees. (which makes sense) I removed HasChildren from our page, and it is working as expected.
Thanks!
Yes, indeed, the item.HasChildren is used when having a load on demand tree, so it knows how to render those nodes that could be expanded and those, that could not.
Regards,
Veselin Tsvetanov
Telerik by Progress