Bind RadTreeView SelectedItems into ViewModel's Collection

3 posts, 1 answers
  1. Nalaka
    Nalaka avatar
    12 posts
    Member since:
    Mar 2014

    Posted 16 Feb 2015 Link to this post

    Hi,

    I'm using RadTreeView in a MVVM application. When I try to bind SelectedItems property in TreeView into Collection in ViewModel, it says ''The property SelectedItems doen't have an accesible setter'. since setter is privete. Is there any way that I can bind the selectedItems to a collection in ViewModel?
  2. Answer
    Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 19 Feb 2015 Link to this post

    Hello Nalaka,

    This is very common scenario for Silverlight ItemsControls (not only Telerik controls) that provide SelectedItem and SelectedItems properties as they cannot be assigned to in XAML - they are read-only and you cannot bind two-way to them.
    There are two ways you can choose from to solve this without breaking the MVVM pattern:
    1. Add IsSelected boolean property to your Business Objects class and use it to know which Items are selected. 
    2. Implement a custom behaviour, commonly referred to as Blend behaviour. You define a Blend behaviour by creating a non-static class that derives from the System.Windows.Interactivity.Behavior<T> class. Note that this abstract base class doesn’t ship with the .NET Framework but it is part of the Microsoft Expression Blend SDK that can be downloaded from Microsoft’s official website here. You will need to reference the System.Windows.Interactivity.dll from your application or class library. You can then easily add the above behaviour to the TreeView control as shown in the markup below:

    <telerik:RadTreeView xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
              ItemsSource="{Binding Items}"
    <i:Interaction.Behaviors>
       <behaviors:SelectedSyncBehavior SelectedItems="{Binding ViewModel.SelectedItems}" />
    </i:Interaction.Behaviors>
      ....
    </telerik:RadTreeView>

    Both approaches are mentioned (together with code snippets) in this stackoverflow post. Use whatever is more suitable for your scenario.

    Regards,
    Evgenia
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft banner
  4. Nalaka
    Nalaka avatar
    12 posts
    Member since:
    Mar 2014

    Posted 20 Feb 2015 in reply to Evgenia Link to this post

    Hi Evgenia,

    Thank you. it worked.
Back to Top