locked
How to Implement virtualization on a scrollviewer showing two listviews. RRS feed

  • Question

  • Hi,

    I have a ScrollViewer which contains two list views. When scrolling two listviews scroll together. (Both listviews have same no of items and bound to different datasource). It seems that due to putting listviews in scrollviewer virtualization is not working. How can I turn it on, is there any way I can Implement custom virtualization. Note that I can not rely on listview's scrolling as I want both of listviews to scrolltogether.

    Any help is appreciated.
    Thanks,
    Naveen
    • Edited by Naveen Dhaka Monday, June 23, 2008 6:13 AM editing
    Monday, June 23, 2008 6:12 AM

Answers

  • Here is one possible approach to get reference to the ScrollBars:

    listView.Loaded += delegate
    {
        ScrollViewer scrollViewer = listView.GetVisualChild<ScrollViewer>();
        if (scrollViewer != null)
        {
            ScrollBar hScrollBar = scrollViewer.Template.FindName("PART_HorizontalScrollBar", scrollViewer) as ScrollBar;
            ScrollBar vScrollBar = scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer) as ScrollBar;
        }
    };

    public static T GetVisualChild<T>(this Visual referenceVisual) where T : Visual
    {
        Visual child = null;
        for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
        {
            child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
            if (child != null && (child.GetType() == typeof(T)))
            {
                break;
            }
            else if (child != null)
            {
                child = GetVisualChild<T>(child);
                if (child != null && (child.GetType() == typeof(T)))
                {
                    break;
                }
            }
        }
        return child as T;
    }

    Hope this helps
    • Marked as answer by Marco Zhou Friday, June 27, 2008 10:35 AM
    Wednesday, June 25, 2008 10:28 AM

All replies

  • Below is how I have placed these ListViews:

    <
    ScrollViewer Width="175" Height="268" x:Name="_outerScrollViewer">
    <StackPanel Orientation="Horizontal">

    <ListView x:Name="_listView" ItemsSource="{Binding}">
    <ListView.View><GridView><GridViewColumn DisplayMemberBinding="{Binding}">
    <GridViewColumnHeader Visibility="Hidden"></GridViewColumnHeader>
    </GridViewColumn>
    </GridView>
    </ListView.View>
    </ListView>

    <ListView x:Name="_otherListView" ItemsSource="{Binding}">
    <ListView.View>
    <GridView>
    <GridViewColumn DisplayMemberBinding="{Binding}">
    <GridViewColumnHeader Visibility="Hidden"></GridViewColumnHeader>
    </GridViewColumn>
    </GridView>
    </ListView.View>
    </ListView>

    </StackPanel>
    </ScrollViewer>

    • Proposed as answer by Naveen Dhaka Wednesday, June 25, 2008 10:10 AM
    • Unproposed as answer by Naveen Dhaka Wednesday, June 25, 2008 10:10 AM
    Monday, June 23, 2008 9:52 AM
  • You can bind the Value property of inner ScrollBars to the Value property of the outer ScrollBars, so that both ListView's ScrollViewers can scroll together.

    Hope this helps
    Wednesday, June 25, 2008 9:59 AM
  •  Marco,

    Thanks for reply, How can I get innerScrollbars of ListView? I couldnot find find any property on ListView to access scrollbars.
    Wednesday, June 25, 2008 10:07 AM
  • Here is one possible approach to get reference to the ScrollBars:

    listView.Loaded += delegate
    {
        ScrollViewer scrollViewer = listView.GetVisualChild<ScrollViewer>();
        if (scrollViewer != null)
        {
            ScrollBar hScrollBar = scrollViewer.Template.FindName("PART_HorizontalScrollBar", scrollViewer) as ScrollBar;
            ScrollBar vScrollBar = scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer) as ScrollBar;
        }
    };

    public static T GetVisualChild<T>(this Visual referenceVisual) where T : Visual
    {
        Visual child = null;
        for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceVisual); i++)
        {
            child = VisualTreeHelper.GetChild(referenceVisual, i) as Visual;
            if (child != null && (child.GetType() == typeof(T)))
            {
                break;
            }
            else if (child != null)
            {
                child = GetVisualChild<T>(child);
                if (child != null && (child.GetType() == typeof(T)))
                {
                    break;
                }
            }
        }
        return child as T;
    }

    Hope this helps
    • Marked as answer by Marco Zhou Friday, June 27, 2008 10:35 AM
    Wednesday, June 25, 2008 10:28 AM