Binding RadTreeView to Property in ViewModel

4 posts, 0 answers
  1. Scott
    Scott avatar
    4 posts
    Member since:
    Nov 2010

    Posted 16 Nov 2010 Link to this post

    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. 
  2. Scott
    Scott avatar
    4 posts
    Member since:
    Nov 2010

    Posted 17 Nov 2010 Link to this post

    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
  3. DevCraft banner
  4. Scott
    Scott avatar
    4 posts
    Member since:
    Nov 2010

    Posted 17 Nov 2010 Link to this post

    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
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 Nov 2010 Link to this post

    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
Back to Top