locked
Detect when Listview ItemsTemplate usercontrol recycles user control. RRS feed

  • Question

  • I have a virtualizing ListView control with a user control as the items template. When the user control is loaded an async call is made that I'd like to cancel if the user control is out of view. 

    I tried using Unloaded but this is not called, even though the items stack panel's cache no longer contains that item. What event will notify the C# when a user control leaves the item stack panels cache, or list view's viewable area?

    I tried using container content changing but it only shows which items are coming into view, but does not show when the container items leaves view.

    Thank you :)

    Here is the XAML.

    <ListView ItemsSource="{Binding MyItemsObs}"
                      ContainerContentChanging="ListViewBase_OnContainerContentChanging"
                      Loaded="ListView_OnLoaded"
                      Style="{StaticResource ListViewStyle1}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <local:MyItemUC/>
                    </DataTemplate>
                </ListView.ItemTemplate>
    </ListView>

    You can get the items stack panel by adding a loaded event in the listview template, or use some visual tree helper extension like win rt xaml toolkit. 

    private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
    {
        _itemsStackPanel = sender as ItemsStackPanel;
        _itemsStackPanel.CacheLength = 0;
    }


    • Edited by Bryan Stump Thursday, February 6, 2014 7:06 PM
    Thursday, February 6, 2014 7:02 PM

Answers

  • Posting for anyone that's using this as a reference.

    There's a method in my user control which is called in phase 1 of container content changing. Inside of this method on the UC, pass in a guid/or the list view item's view model. Then the UC can check if the value is different. If the value is different I am going to cancel the task in the old view model.

    • Marked as answer by Bryan Stump Thursday, February 6, 2014 8:59 PM
    Thursday, February 6, 2014 8:59 PM

All replies

  • Posting for anyone that's using this as a reference.

    There's a method in my user control which is called in phase 1 of container content changing. Inside of this method on the UC, pass in a guid/or the list view item's view model. Then the UC can check if the value is different. If the value is different I am going to cancel the task in the old view model.

    • Marked as answer by Bryan Stump Thursday, February 6, 2014 8:59 PM
    Thursday, February 6, 2014 8:59 PM
  • Thank you for sharing your solution with us :) Should help someone who meet the same issue.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, February 7, 2014 2:01 AM
    Moderator