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

Maintaining selected item on refresh ?

18 Answers 1332 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Barry
Top achievements
Rank 1
Barry asked on 14 Mar 2019, 07:19 PM

I have a ReadTreeListView with two level of items. When a sub-level item is selected I can see no way to maintain that selection when the data source is updated (items added or removed). *See attached picture*. When the data source is updated I lose my selection. Using "EnableLostFocusSelectedState = false" helped with the top level items, but not the inner items.

Please advise.

Barry

18 Answers, 1 is accepted

Sort by
0
Barry
Top achievements
Rank 1
answered on 14 Mar 2019, 07:24 PM

Just another point - After the source has been updated (item add/remove) I do have a handle on what the SelectedItem was, I just can't get it to show as such in the TreeView after it refreshes.

Thanks,

Barry

0
Vladimir Stoyanov
Telerik team
answered on 19 Mar 2019, 11:58 AM
Hello Barry,

Thank you for the attached picture. 

I tested the described scenario, however I was not able to reproduce the same result. On my end, when the collection bound to the RadTreeListView is updated, the selection is preserved. I am attaching the sample project which I used for testing purposes. Please, check it out and see how it differs from the setup on your end.

May I ask you to let me know how I can reproduce the observed on your end result? This will hopefully allow me to investigate the scenario and better assist you.

I am looking forward to your reply.

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Barry
Top achievements
Rank 1
answered on 21 Mar 2019, 02:41 PM

Thanks for the demo app. I'm currently looking at it and trying to get it to run - 

SeverityCodeDescriptionProjectFileLineSuppression State
ErrorThe tag 'RadTreeListView' does not exist in XML namespace 'http://schemas.telerik.com/2008/xaml/presentation'. Line 14 Position 10.ExpandItemsIntoView_WPFC:\Users\bjo0714\Downloads\f9fc89ab-8347-4878-8ce3-13ea2963c5df_TreeListViewUpdateChildCollection\MainWindow.xaml14

 

I copied in and referenced all our Telerik stuff, so not sure why.


0
Barry
Top achievements
Rank 1
answered on 21 Mar 2019, 03:16 PM
ahh...I had to target Framework 4.5 :-)
0
Barry
Top achievements
Rank 1
answered on 21 Mar 2019, 03:28 PM

That is some interestingly formatted code :-)

The scenario in your app isn't quite the same as what I'm dealing with. We have multiple tabs in our app, and when we go to another tab that allows modification of our "list" from the TreeView tab, a call is sent back to the previous tab with the to reload the TreeView...parent and child data alike. It's like loading it for the first time really, and the selection is lost *visually*.

Getting the Treeview to hi-light the selection in the standard blue colour is eluding me....the same way as if you clicked on it with the mouse. After the data source is adjusted and the TreeView reloaded I do still have a handle on what was selected previously ("selectedItem")...I just can't get the TreeView to select it (show it) the same way as if it was clicked on with the mouse.

0
Vladimir Stoyanov
Telerik team
answered on 25 Mar 2019, 02:29 PM
Hello Barry,

Before we continue I just want to make sure that you are referring to the RadTreeListView component and not the RadTreeView component. I will proceed with the RadTreeListView in mind, however feel free to correct, if I am wrong on that. 

My current understanding is that you are reloading the data displayed in the RadTreeListView and you want to set its SelectedItem property, however the UI is not reflecting this accordingly.

I modified the sample project a bit to try and come closer to the described scenario, however I was still not able to reproduce the same result. On my end, the UI is updated with the correct SelectedItem. Please, check out the modified sample project and let me know what I am missing in order to recreate the same scenario on my end. This will hopefully allow me to investigate it and further assist you.

I am looking forward to your reply.

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Barry
Top achievements
Rank 1
answered on 25 Mar 2019, 04:58 PM

I tried your demo ( RadTreeListView is correct).  Just to make sure I understand its intent correctly; clicking on the "Reload RadTreeListView" is meant to randomly select another row in the tree and hi-light it ?

It looks like it's rebinding a randomly generated list (or varying length) to the VM, and then selecting one at random. It does show the selected item as blue (as in the same as a mouse click on it). HHmmm....so perhaps on my end/app...it's doing something after the fact to deselect it from the control, even though it stays selected in the VM.

Ok, it seems you've proven the control is doing the right thing, and it's us over on our end. I'll keep digging to see what happens

0
Vladimir Stoyanov
Telerik team
answered on 26 Mar 2019, 02:33 PM
Hello Barry,

Indeed you have understood the intention correctly. The idea was to try and replicate the described scenario of refreshing the ItemsSource and setting the SelectedItem. 

Should you manage to find a way to reproduce the observed on your end scenario in the sample project, feel free to write back and I will gladly investigate the scenario further.

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Barry
Top achievements
Rank 1
answered on 28 Mar 2019, 02:54 PM

This is probably a dumb question, but does it matter if the Items" are a List or an ObservableCollection ?

We use a List<TreeUnitViewModel> and you use a ObservableCollection<MyObject>.

0
Barry
Top achievements
Rank 1
answered on 28 Mar 2019, 03:05 PM

This is our TreeListView:

 

    <telerik:RadTreeListView x:Name="ProjectsTreeView" Grid.Row="0" Width="203" SelectionMode="Single" 
                        telerik:PersistenceManager.StorageId="STORE_ProjectsTreeView" 
                        SelectedItem="{Binding SelectedTreeViewItem, Mode=TwoWay}" 
                        ItemsSource="{Binding ProjectData}" 
                        RowIndicatorVisibility="Collapsed" 
                        CanUserFreezeColumns="False"
                        CanUserResizeColumns="False"
                        CanUserResizeRows="False"
                        CanUserReorderColumns="False"
                        CanUserSortColumns="True"
                        CanUserDeleteRows="False"
                        Sorted="ProjectsTreeView_Sorted"
                        AutoGenerateColumns="False"
                        AutoExpandItems="False"
                        AutoExpandGroups="True"
                        IsExpandedBinding="{Binding IsExpanded, Mode=TwoWay}"
                        IsExpandableBinding="{Binding IsExpandable}"
                        GridLinesVisibility="None"
                        IsSynchronizedWithCurrentItem="False"
                        VerticalAlignment="Stretch"  
                        EnableLostFocusSelectedState="False"
                        local:ScrollJumpDependencyProperties.FixJump="True"
                        IsFilteringAllowed="False" CanUserSortGroups="False">
                <telerik:RadTreeListView.Resources>
                    <Style TargetType="{x:Type telerik:TreeListViewRow}">
                        <!--<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>-->
                    </Style>
                    <Style TargetType="{x:Type telerik:GridViewCell}" BasedOn="{StaticResource TreeViewCell}">
                        <EventSetter Event="UIElement.PreviewMouseLeftButtonUp" Handler="ProjectsTreeView_PreviewMouseUp"/>
                    </Style>
                </telerik:RadTreeListView.Resources>
                <telerik:RadTreeListView.HierarchyChildTemplate>
                    <StaticResource ResourceKey="Unit"/>
                </telerik:RadTreeListView.HierarchyChildTemplate>
                <telerik:RadTreeListView.ChildTableDefinitions>
                    <telerik:TreeListViewTableDefinition ItemsSource="{Binding UnitData}"/>
                </telerik:RadTreeListView.ChildTableDefinitions>
                <telerik:RadTreeListView.Columns>
                    <telerik:GridViewDataColumn x:Name="ProjectsTreeViewColumn" DataMemberBinding="{Binding Name}"
                        CellTemplateSelector="{StaticResource CellTypeDataTemplateSelector}"
                        HeaderCellStyle="{StaticResource STORE_GridViewHeaderCellStyle}" 
                        Header="{x:Static InstantelBWResources:Resources.EventManagerView_TreeviewTitle}"
                        IsResizable="False"
                        MinWidth="201"
                        IsReadOnly="True" />
                </telerik:RadTreeListView.Columns>
            </telerik:RadTreeListView>

 

...if that helps in your analysis.

We use telerik:PersistenceManager.....which has me wondering if that's the issue.

0
Barry
Top achievements
Rank 1
answered on 28 Mar 2019, 06:47 PM
One point though....if I select a parent item the selection sticks with the refresh...it's lost when on a child item when the refresh (rebinding) of the TreeView occurs. The randomizing in your sample app doesn't seem to hang onto child selections either...if the button is pressed a new randomized list may be smaller than the previous one, and a selection lost....or am I missing something ?
0
Vladimir Stoyanov
Telerik team
answered on 02 Apr 2019, 08:19 AM
Hello Barry,

I will address your replies in order.

There isn't any difference between the List and ObservableCollection when it comes to the selection. The only difference is that the ObservableCollection will send notifications when its updated. 

As for persisting the RadTreeListView, this should not cause any problems with the selection mechanism.

May I ask you to clarify what you mean by "if the button is pressed a new randomized list may be smaller than the previous one, and a selection lost"? Can you elaborate on the exact scenario so that I can check it out on my end?

I am looking forward to your reply.

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Barry
Top achievements
Rank 1
answered on 23 Apr 2019, 05:51 PM
So I'm back to looking at this again....and you're example isn't maintaining a nested selection when the button is hit (re-loaded). Anyway, iy seems that's it's all about whatever params are set to in the XAML.
0
Vladimir Stoyanov
Telerik team
answered on 25 Apr 2019, 01:04 PM
Hello Barry,

Thanks for the update.

I investigated the scenario further and indeed nested items will be selected only if they are expanded before that. It turns out that we have an item logged in our feedback portal for this scenario: TreeListView: Hierarchical items are not selected if they are not expanded. You can follow the item in order to get notified for any developments. 

For the time being, I cannot suggest a viable workaround for this behavior, except expanding the items before selecting them. Please, excuse me for any inconvenience caused. 

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Barry
Top achievements
Rank 1
answered on 25 Apr 2019, 03:08 PM

Thanks for your great assistance with this issue. We just upgraded to: Telerik_UI_for_WPF_2018_2_827_Dev_Hotfix.

Maybe I'll have better luck with a newer version of Telerik, otherwise our users will just have to live with losing their selection when they move around and the TreeView gets reloaded.

Barry

0
Barry
Top achievements
Rank 1
answered on 25 Apr 2019, 03:31 PM
One last thing...the bindings are all there after the RadTreeView reloads, and the SelectedItem is as well....is there any way from code-behind to simply force the control to look like the selected item is selected ?..as if it was clicked on with the mouse ?...because another way to look at is the control loses the "visual appearance" of it being selected after reloading, even though from a bindings point of view in the code-behind the last selection is still the SelectedItem.
0
Barry
Top achievements
Rank 1
answered on 25 Apr 2019, 07:14 PM

THIS IS THE FIX:

 

private void ProjectsTreeView_DataLoaded(object sender, EventArgs e)
        {
            EventManagerViewModel emvm = this.DataContext as EventManagerViewModel;
            if (emvm.SelectedTreeViewItem != null && emvm.SelectedTreeViewItem.IsSelected && emvm.SelectedTreeViewItem is TreeUnitViewModel)
            {
                ProjectsTreeView.SelectedItems.Add(emvm.SelectedTreeViewItem);
            }
        }

 

...it appears assigning my object to SelectedItem isn't enough, it also has to be added to SelectedItems collection !

0
Accepted
Vladimir Stoyanov
Telerik team
answered on 30 Apr 2019, 09:51 AM
Hello Barry,

Thank you for the updates. 

I am glad to hear that you were able to achieve what you were going for. Thank you for the sharing your approach with the community.

Regards,
Vladimir Stoyanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
TreeListView
Asked by
Barry
Top achievements
Rank 1
Answers by
Barry
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Share this question
or