locked
How to get ListView visible items?

    Question

  • I'm trying to create a windows 8 metro style app. I'm using a Windows::UI::Xaml::Controls::ListView object, how can I get either indexes of visible items or items themselves in list view (actually for all items visibility attribute is set to visible, but I need to get items which are currently shown and can be viewed on desktop), or at least to know if the certain ListViewItem is visible on the desktop now, or not?

    Thanks In Advance
    Thursday, August 23, 2012 12:14 PM

Answers

  • Your right, my bad. In addition to verifying that the item is HitTestVisibleyou could confirm the item is in the bounds of the window content.

     
    bool MainPage::IsInBounds(FrameworkElement^ element)
    {
        Point point;
        point.X = 0;
        point.Y = 0;

        Rect bounds = Window::Current->Bounds;
        GeneralTransform^ gt = element->TransformToVisual(Window::Current->Content);

        Point offset = gt->TransformPoint(point);

        bool xResult = offset.X + element->ActualWidth >= 0 && offset.X < bounds.Width;
        bool yResult = offset.Y + element->ActualHeight >= 0 && offset.Y < bounds.Height;

        return xResult && yResult;
    }

     


    Matt Hohn

    • Marked as answer by Sedrak Tuesday, August 28, 2012 7:04 AM
    Friday, August 24, 2012 5:09 PM
  • I am pretty sure the IsHitTestVisible property will work for you.

    for(int i=0; i < (int)listview->Items->Size; i++)

    {

        DependencyObject^ depobject = listview->ItemContainerGenerator->ContainerFromIndex(i);

        bool isVisible = (bool)depobject->GetValue(IsHitTestVisibleProperty);

    }


    Matt Hohn

    • Proposed as answer by Jesse Jiang Friday, August 24, 2012 6:12 AM
    • Marked as answer by Sedrak Tuesday, August 28, 2012 7:04 AM
    Thursday, August 23, 2012 5:35 PM

All replies

  • If I understand correctly, you can access the ListViewItem as :-

    //get the object
    DependecyObject obj^ = ListView->ItemContainerGenerator->ContainerFromIndex(index);
    
    
    //get object's visibility
    
    Platform::Object^ Value = obj->GetValue(VisibilityProperty);
    
    

    -Sagar

    • Proposed as answer by Jesse Jiang Friday, August 24, 2012 6:12 AM
    Thursday, August 23, 2012 2:06 PM
    Moderator
  • I think he need to know what items are currently in sight.
    Thursday, August 23, 2012 4:26 PM
  • I am pretty sure the IsHitTestVisible property will work for you.

    for(int i=0; i < (int)listview->Items->Size; i++)

    {

        DependencyObject^ depobject = listview->ItemContainerGenerator->ContainerFromIndex(i);

        bool isVisible = (bool)depobject->GetValue(IsHitTestVisibleProperty);

    }


    Matt Hohn

    • Proposed as answer by Jesse Jiang Friday, August 24, 2012 6:12 AM
    • Marked as answer by Sedrak Tuesday, August 28, 2012 7:04 AM
    Thursday, August 23, 2012 5:35 PM
  • Thanks for response very much, but actually I need to know which items are currently in sight, the ones I can see on the screen.
    Friday, August 24, 2012 8:00 AM
  • Your right, my bad. In addition to verifying that the item is HitTestVisibleyou could confirm the item is in the bounds of the window content.

     
    bool MainPage::IsInBounds(FrameworkElement^ element)
    {
        Point point;
        point.X = 0;
        point.Y = 0;

        Rect bounds = Window::Current->Bounds;
        GeneralTransform^ gt = element->TransformToVisual(Window::Current->Content);

        Point offset = gt->TransformPoint(point);

        bool xResult = offset.X + element->ActualWidth >= 0 && offset.X < bounds.Width;
        bool yResult = offset.Y + element->ActualHeight >= 0 && offset.Y < bounds.Height;

        return xResult && yResult;
    }

     


    Matt Hohn

    • Marked as answer by Sedrak Tuesday, August 28, 2012 7:04 AM
    Friday, August 24, 2012 5:09 PM
  • Thanks a lot, that really worked
    Tuesday, August 28, 2012 7:05 AM