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

Binding to RadTreeViewItem IsSelected

4 Answers 356 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 16 Apr 2015, 09:07 PM

I need to select an item in the tree and I am using ItemSource to generate the elements on the tree so I do not have a reference to the node.

I tried to use this example but IsSelected in my view model is not set or retreived:

http://www.telerik.com/forums/radtreeview-selecteditem-binding

My code:

                            <telerik:RadTreeListView x:Name="treeFileSystem" IsBusy="{Binding IsBusy}" RowIndicatorVisibility="Collapsed"    
                                   IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding FoldersOnDemand, IsAsync=True}" IsExpandedBinding="{Binding IsExpanded, Mode=TwoWay}"
                                   AutoExpandItems="False"  Grid.Row="0" SelectionChanged="treeFileSystem_SelectionChanged">
                                <telerik:RadTreeListView.ItemContainerStyle>
                                    <Style TargetType="telerik:RadTreeViewItem">
                                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                                    </Style >
                                </telerik:RadTreeListView.ItemContainerStyle>

What it the correct approach to select the item in the tree?  I know the full path and I know which item it is in the view model.

 

Thanks

4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 20 Apr 2015, 03:26 PM
Hi Kevin,

A way of getting the selected item of RadTreeListView is by using the SelectedItem property of the control. Please check our documentation on Selection for further reference.

I am also attaching a sample project illustrating how the property can be used.

I hope this helps.

Best Regards,
Stefan
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Kevin
Top achievements
Rank 1
answered on 22 Apr 2015, 10:26 PM

Thanks for the assistance.  I set the SelectedItem and in the debugger see that it is set but the item is not visually selected in the tree and the SelectionChanged() event does not fire.  Any tips on why this would be?

Thanks

0
Kevin
Top achievements
Rank 1
answered on 23 Apr 2015, 02:54 PM

I have been doing more research and I found the issue but I am not sure how to fix it.

I dynamically load the tree when nodes are opened.  I have added a search capability which automatically opens the tree for the user based upon the item they picked in a RadAutoComplete box.  The tree opens fine but the item is not selected.  I open the tree in the RadAutoCompleteBox's selectionChanged event.  I load the ViewModel items the user picked and set IsExpanded to true.  The GUI does not draw the tree opening until the selectionChanged event finishes.  After creating the ViewModel objects I set the tree's SelectedItem in the RadAutoCompleteBox selectionChanged and it appears that it does not apply since the tree has not opened yet.

If I manually open the tree, use the AutoComplete to search for a node and select it, the node is selected and the tree's SelectionChanged fires. 

 Example:

folderAutoCompleteBox_SelectionChanged

loop through ViewModel creating new objects and setting their IsExpanded to true

When the loop is done set the tree.SelectedItem = the object the user selected

End of SelectionChanged

 

It appears that I need to set SelectedItem after the tree has opened all of the nodes.  Any opinion on a better way to do this?

0
Stefan
Telerik team
answered on 24 Apr 2015, 03:16 PM
Hi Kevin,

In order to ensure that RadTreeListViewItem is expanded before setting the SelectedItem property, I suggest you using the Dispatcher class. I am attaching a sample project illustrating how the approach could be implemented.

I hope this helps.

Best Regards,
Stefan X1
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
TreeListView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Kevin
Top achievements
Rank 1
Share this question
or