开发者

How to Know the lastVisbile row in wpf toolkit datagrid

i have datagrid which has 200 rows of data. i need to display the records in 4 monitors. monitor resolutions may be different. so, i need to display the records in such a way that suppose 40 records can fit in first monitor and the i need to take the remaining records into the second monitor like that.

how i can know the last visible row in the screen.so that i can take the count and push the remaining data to next m开发者_StackOverflow中文版onitor.

or Any other approach also will be helpful.


This is not easy to do! A few months back I wrote a utility method for a Windows Phone 7 application that enumerates the items that are currently visible within an ItemsControl:

    /// <summary>
    /// Enumerates all the items that are currently visible in an ItemsControl. This
    /// implementation works for both virtualized and non-virtualized panels.
    /// </summary>
    public static IEnumerable<FrameworkElement> GetItemsInView(this ItemsControl itemsControl)
    {
        // find the panel that hosts our items - this is 'cached'
        // using the ItemsControl.Tag property to minimize visual tree
        // navigation
        Panel itemsHostPanel = itemsControl.Tag as Panel;
        if (itemsHostPanel == null)
        {
            itemsHostPanel = itemsControl.Descendants<Panel>()
                                            .Cast<Panel>()
                                            .Where(p => p.IsItemsHost)
                                            .SingleOrDefault();
            itemsControl.Tag = itemsHostPanel;
        }

        VirtualizingStackPanel vsp = itemsHostPanel as VirtualizingStackPanel;
        if (vsp != null)
        {
            // implementation for virtualizing lists
            return GetItemsInView(itemsControl, vsp);
        }
        else
        {
            // implementation for non-virtualizing lists
            return Enumerable.Range(0, itemsControl.Items.Count)
                          .Select(index => itemsControl.ItemContainerGenerator.ContainerFromIndex(index))
                          .Cast<FrameworkElement>()
                          .Where(container => container.GetRelativePosition(itemsControl).Y + container.ActualHeight > 0)
                          .Where(container => container.GetRelativePosition(itemsControl).Y < itemsControl.ActualHeight);
        }
    }

    /// <summary>
    /// Gets the items in view for a virtualizing list
    /// </summary>
    private static IEnumerable<FrameworkElement> GetItemsInView(this ItemsControl itemsControl, VirtualizingStackPanel vsp)
    {
        // iterate over each of the items in view
        int firstVisibleItem = (int)vsp.VerticalOffset;
        int visibleItemCount = (int)vsp.ViewportHeight;
        for (int index = firstVisibleItem; index <= firstVisibleItem + visibleItemCount + 2; index++)
        {
            var item = itemsControl.ItemContainerGenerator.ContainerFromIndex(index) as FrameworkElement;
            if (item == null)
                continue;

            yield return item;
        }
    }

This should work for a WPF DataGrid. As you can see the implementation depends on whether the view is virtualized or not.

Note, this code is part of the WP7Contrib project, and can be seen here. It has dependencies on a few other utility / extension methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜