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

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
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

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.


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.
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

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
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

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>.

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.

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

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

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


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 !
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