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

ItemsControlSelector.cachedSelectedItems not behaving properly

5 Answers 50 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Carlos Serrano
Top achievements
Rank 1
Carlos Serrano asked on 21 Oct 2013, 09:58 AM
Hello,

I have a RadListBox (single selection) displaying a number of items, with one of the items being selected, and at some point in time I set the ItemsSource to null. Then when I look at what is held onto in a profiler, I see that ItemsControlSelector.cachedSelectedItems still points to the last selected item, which is a problem for us (SelectedItems is empty, SelectedItem is null).

I have not been able to find a proper workaround to clear cachedSelectedItems when the ItemsSource becomes null. Would you have any tip?

Thanks,
Carlos.

5 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 24 Oct 2013, 11:02 AM
Hi Carlos,

RadListBox control caches the selected items when clearing the ItemsSource because if a new collection is set to the ItemsSource and it contains the items, they must be selected again. If the control unloads, the items will be disposed if they don't have references to other objects.

Hope this helps.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Carlos Serrano
Top achievements
Rank 1
answered on 24 Oct 2013, 02:48 PM
Thanks George.

Unfortunately, the ItemsControlSelector.cachedSelectedItems are not cleared when the control is unloaded. Would you have any other suggestion that would allow me to clear that list?

Regards,
Carlos
0
George
Telerik team
answered on 29 Oct 2013, 02:34 PM
Hi Carlos,

I tried to reproduce the memory leak in a sample project, but to no avail. Can you please refer to the attached project and video and let me know if I missed something. I am glad to assist you further.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Carlos Serrano
Top achievements
Rank 1
answered on 30 Oct 2013, 08:49 AM
Hello George,

Please find attached a very simple test case. It starts with an empty RadListBox, and 3 buttons.
- The first button fills the RadListBox with 100 items
- The second button sets the ItemsSource to null
- The third button removes and re-adds the RadListBox to the visual tree

(1) Launch the example in the debugger and put a breakpoint in every method of MainPage.xaml.cs.
(2) Click FillRadListBox.
(3) Select "Item 4"
(4) Click Empty RadListBox --> once the ItemsSource is set to null, you can see that RadListBox.cachedSelectedItem is "Item 4" and cachedSelectedItems has one item, "Item 4"
(5) Now click Unload RadListBox --> when you break into _onUnloaded, you can see, again, that cachedSelectedItem and cachedSelectedItems have the same value

Which means that emptying the RadListBox or unloading it does not reset cachedSelectedItem or cachedSelectedItems.

Can you confirm this? Do you have any suggestion?

Thank you!
Carlos.

P.S. I could not attach any zip file, so I unfortunately have to paste the code below...

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace RadListBox_cachedSelectedItems
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();

ListBox.Unloaded += _onUnloaded;
}

private void _onUnloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Unloaded the RadListBox");
}

private IEnumerable<string> _createData()
{
var data = new ObservableCollection<string>();
for (var i = 0; i < 100; i++) {
data.Add("Item " + i);
}
return data;
}

private void _fillListBox(object sender, RoutedEventArgs e)
{
ListBox.ItemsSource = _createData();
}

private void _emptyListBox(object sender, RoutedEventArgs e)
{
ListBox.ItemsSource = null;
}

private void _unloadListBox(object sender, RoutedEventArgs e)
{
LayoutRoot.Children.Remove(ListBox);
LayoutRoot.Children.Insert(0, ListBox);
}
}
}

<UserControl x:Class="RadListBox_cachedSelectedItems.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        
        <telerik:RadListBox x:Name="ListBox" Height="150" Width="300"/>
        
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="Fill RadListBox" Click="_fillListBox"/>
            <Button Content="Empty RadListBox" Click="_emptyListBox"/>
            <Button Content="Unload RadListBox" Click="_unloadListBox"/>
        </StackPanel>
    </Grid>
</UserControl>

0
George
Telerik team
answered on 04 Nov 2013, 12:32 PM
Hello Carlos,

Thank you for the provided sample code. Your remarks are correct and indeed the selected items are kept. By "unloading" I meant disposing - when the RadListBox control is disposed, the cachedSelectedItems will be clear and they will not cause memory leaks. However, until the control instance is alive, the items are cached. I logged this work item here where you can track its progress - http://www.telerik.com/support/pits.aspx#/public/silverlight/16091 . We will definitely look into it in our further development.

I am glad to update your Telerik points as well. Hope this helps.

Please, note that forum threads support uploading only image formats. In order to upload a zip file you could open a support ticket as well.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
ListBox
Asked by
Carlos Serrano
Top achievements
Rank 1
Answers by
George
Telerik team
Carlos Serrano
Top achievements
Rank 1
Share this question
or