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

Bind RadTreeView SelectedItems into ViewModel's Collection

2 Answers 494 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Nalaka
Top achievements
Rank 1
Nalaka asked on 17 Feb 2015, 05:03 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Evgenia
Telerik team
answered on 19 Feb 2015, 02:04 PM
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.

 
0
Nalaka
Top achievements
Rank 1
answered on 20 Feb 2015, 10:20 AM
Hi Evgenia,

Thank you. it worked.
Tags
TreeView
Asked by
Nalaka
Top achievements
Rank 1
Answers by
Evgenia
Telerik team
Nalaka
Top achievements
Rank 1
Share this question
or