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

Binding RadTreeView to Property in ViewModel

3 Answers 95 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 17 Nov 2010, 04:34 AM
What is the easiest way to bind a TreeView to a Property in the ViewModel?  I first tried to implement the exmaple in the help docs by Binding to Object and using a hierarchical data template to display in the TreeView, but it's not working and I don't know enough about binding, resources and templates to know why.

I would like to bind my TreeView to an object and have the treeview reflect the changes as they are made in code.  Is this possible, and if so, can you point me in the right direction?

Thanks,

-Scott

EDIT:  I wanted to add that within my ViewModel, I am creating and maintaining an object based on a Ria Services EF call.  I have an object with the full hierarchy I need.  I just need to find a way to bind it to the TreeView. 

3 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 17 Nov 2010, 06:30 AM
I got it worked out for the most part.  I created a ObservableCollection that contains my elements in a heirarchy format.  I then hung that off of my ViewModel and bound to it in the hierarchical template.  I now have a working TreeView that binds to my ViewModel for its data - I'm pretty happy about that - baby steps are fine... as long as they are forward.

Speaking for forward.  I used to have the SelectedItem bound to my ViewModel and working just fine as well.  Since making these changes the getter in my RadTreeViewItem property is being called, but the setter is never being called.  As the application launches the getter gets called one time, but no matter what element on the treeview I select, the setter never gets called.

Any ideas and what may be causing this?

Thanks,

-Scott
0
Scott
Top achievements
Rank 1
answered on 17 Nov 2010, 07:58 PM

I'm stil having the issue where the SelectedItem is not binding correctly to my ViewModel.  Here is my XAML:

<Grid x:Name="projectViewLayout">
<telerik:RadTreeView  ItemTemplate="{StaticResource RootTemplate}" ItemsSource="{Binding ProjectTreeModel}" SelectedItem="{Binding SelectedProjectItem, Mode=TwoWay}" />                                
</Grid>

As you can see, I am binding the SelectedItem of the TreeView to the SelectedProejctItem of my ViewModel.  When the page loads and the TreeView is built, it does call the SelectedProjectItem getter (which is null at the time of the call), but it never sets the value.

Any ideas?

-Scott
0
Petar Mladenov
Telerik team
answered on 18 Nov 2010, 06:48 PM
Hello Scott,

From the code snippet you sent us, we are unable to reproduce your issue. So could you please have a look  the attached project and let me know if I am missing something. Also, please keep in mind that the SelectedItem property have to be bound to an item from the ItemsSource collection of the RadTreeView (in your case the SelectedProjectItem should represent one of the ProjectTreeModel items).

You can always feel free to ask if further questions come up.

Also, I am not sure whether you have checked the online help resources for the RadTreeView control, so I will list a few related articles just in case:

Greetings,
Petar Mladenov
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
TreeView
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or