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

Working With Bindable SelectedItems Collection in RadTreeView

2 Answers 262 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Stacey Haslag
Top achievements
Rank 1
Stacey Haslag asked on 16 Feb 2011, 10:12 PM

I am using the RadTreeView control for the first time. I am using the MVVM approach to interact with this control. What I want to do is be able to set and get the SelectedItems of the RadTreeView. I am allowing multiple selections to occur. I have the initial heirarchy list of business objects that I bind to the control. But I can't find a way to neatly interact with a collection of the selected items.

I have read a lot of the various instructions in this area http://www.telerik.com/help/silverlight/radtreeview-how-to-bind-hierarchical-data-use-containerbindingcollection.html. They lead me to using the ContainerBindingCollection and HierarchicalDataTemplate approach. So I have done this, and this is binding my collection of datasource business objects appropriately. 

However, this still does not get me exactly what I want. Maybe it is not possible, but what I would like is a way to bind to the SelectedItem property, and receive a collection of selected items; and vice-verca to Set the SelectedItem list, and have the appropriate items selected.

With the way that it is set up using the ContainerBindingCollection approach, I don't see a way to do that...would I have to build my own collection of selected items one-by-one as they select them?

Here is my XAML:

<telerik:ContainerBindingCollection x:Name="BindingsCollection">
            <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding IsSelected, Mode=TwoWay}"></telerik:ContainerBinding>
        </telerik:ContainerBindingCollection>

<telerik:RadTreeView Margin="0, 20, 0, 20" SelectionMode="{Binding SelectionMode}" ItemsSource="{Binding SortedTableOfContentsList}"
                             SelectedItem="{Binding SelectedPage, Mode=TwoWay}">
            <telerik:RadTreeView.ItemTemplate>
                <telerik:HierarchicalDataTemplate ItemsSource="{Binding RelatedPages}" 
                                                  telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}">
                    <StackPanel Orientation="Vertical" VerticalAlignment="Bottom">
                        <TextBlock Text="{Binding Description}"></TextBlock>
                        <TextBlock FontSize="9" Text="{Binding Notes}" Margin="10, 0, 0, 0"></TextBlock>
                    </StackPanel>
                </telerik:HierarchicalDataTemplate>
            </telerik:RadTreeView.ItemTemplate>
        </telerik:RadTreeView>

Notes: I do get all my heirarchy items to show in my tree list appropriately. I do have an "IsSelected" property added to my business object for binding. I am getting my "SelectedPage" property to fire in my view model for each item that is selected...it just fires one at a time as items are selected individually.

Thanks!
Stacey

2 Answers, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 21 Feb 2011, 01:22 PM
Hi Stacey Haslag,

First I would like to note that in Extended selection mode, the SelectedItem is always the first item in the SelectedItems collection: SelectedItems[0]. However, could you please examine this blog post showing a possible way to synchronize the SelectedItems colelction with a collection of business items in the ViewModel.
Please let us know if this approach fits in your scenario.

Kind regards,
Petar Mladenov
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Stacey Haslag
Top achievements
Rank 1
answered on 04 Mar 2011, 02:17 PM
Sorry for the late response to this! Yes, this scenario does fit my situation; I am now able to interact with the SelectedItems collection. Thanks for your help!

Stacey
Tags
TreeView
Asked by
Stacey Haslag
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Stacey Haslag
Top achievements
Rank 1
Share this question
or