ItemsControlSelector.cachedSelectedItems not behaving properly

6 posts, 0 answers
  1. Carlos Serrano
    Carlos Serrano avatar
    12 posts
    Member since:
    May 2010

    Posted 21 Oct 2013 Link to this post

    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.
  2. George
    Admin
    George avatar
    1332 posts

    Posted 24 Oct 2013 Link to this post

    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 >>
  3. DevCraft banner
  4. Carlos Serrano
    Carlos Serrano avatar
    12 posts
    Member since:
    May 2010

    Posted 24 Oct 2013 Link to this post

    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
  5. George
    Admin
    George avatar
    1332 posts

    Posted 29 Oct 2013 Link to this post

    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 >>
  6. Carlos Serrano
    Carlos Serrano avatar
    12 posts
    Member since:
    May 2010

    Posted 30 Oct 2013 Link to this post

    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>

  7. George
    Admin
    George avatar
    1332 posts

    Posted 04 Nov 2013 Link to this post

    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 >>
Back to Top
DevCraft banner