【问题标题】:How to Know the lastVisbile row in wpf toolkit datagrid如何知道 wpf 工具包数据网格中的 lastVisbile 行
【发布时间】:2011-09-29 06:37:01
【问题描述】:

我有包含 200 行数据的数据网格。我需要在 4 个监视器中显示记录。 显示器分辨率可能不同。所以,我需要以这样一种方式显示记录,假设第一个监视器可以容纳 40 条记录,我需要像这样将剩余的记录放入第二个监视器。

我如何知道屏幕上最后一个可见行。以便我可以进行计数并将剩余数据推送到下一个监视器。

或任何其他方法也会有所帮助。

【问题讨论】:

    标签: wpf wpftoolkit


    【解决方案1】:

    这并不容易!几个月前,我为 Windows Phone 7 应用程序编写了一个实用方法,该方法枚举了 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;
            }
        }
    

    应该适用于 WPF DataGrid。如您所见,实现取决于视图是否被虚拟化。

    注意,此代码是 WP7Contrib 项目的一部分,can be seen here。它依赖于其他一些实用程序/扩展方法。

    【讨论】:

    • 感谢您的回复。 coline,所以我需要将网格放在项目控件中。我是 WPF 的新手。如果您有任何示例,我们如何使用这个扩展类会更有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2010-12-19
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2010-12-02
    相关资源
    最近更新 更多