Binding to RadTreeViewItem IsSelected

5 posts, 0 answers
  1. Kevin
    Kevin avatar
    15 posts
    Member since:
    Feb 2014

    Posted 16 Apr 2015 Link to this post

    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

  2. Stefan X1
    Admin
    Stefan X1 avatar
    514 posts

    Posted 20 Apr 2015 Link to this post

    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.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Kevin
    Kevin avatar
    15 posts
    Member since:
    Feb 2014

    Posted 22 Apr 2015 Link to this post

    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

  5. Kevin
    Kevin avatar
    15 posts
    Member since:
    Feb 2014

    Posted 23 Apr 2015 Link to this post

    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?

  6. Stefan X1
    Admin
    Stefan X1 avatar
    514 posts

    Posted 24 Apr 2015 Link to this post

    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.

     
Back to Top
UI for WPF is Visual Studio 2017 Ready