WPF 自定义 VirtualWrapPanel

这是一个关于如何实现WPF中的VirtualWrapPanel的详细教程,包括计算视图范围、定位子元素、虚拟化面板的测量和布局以及IScrollInfo接口的实现。代码示例展示了如何根据可用空间调整子项的排列并进行滚动操作。
摘要由CSDN通过智能技术生成
http://www.codeproject.com/Articles/64865/Enable-MultiSelect-in-WPF-ListView-2
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace QuickZip.UserControls
{
        public class VirtualWrapPanel : VirtualizingPanel, IScrollInfo, IChildInfo
        {
            public VirtualWrapPanel()
            {
                // For use in the IScrollInfo implementation
                this.RenderTransform = _trans;
            }

            #region Methods

            #region Layout specific code
            // I've isolated the layout specific code to this region. If you want to do something other than tiling, this is
            // where you'll make your changes

            /// <summary>
            /// Calculate the extent of the view based on the available size
            /// </summary>
            /// <param name="availableSize">available size</param>
            /// <param name="itemCount">number of data items</param>
            /// <returns></returns>
            private Size CalculateExtent(Size availableSize, int itemCount)
            {
                if (Orientation == Orientation.Horizontal)
                {
                    int childPerRow = CalculateChildrenPerRow(availableSize);
                    return new Size(childPerRow * this.ItemSize.Width,
                        this.ItemSize.Height * Math.Ceiling((double)itemCount / childPerRow));
                }
                else
                {
                    int childPerCol = CalculateChildrenPerCol(availableSize);
                    return new Size(this.ItemSize.Width * Math.Ceiling((double)itemCount / childPerCol),
                        childPerCol * this.ItemSize.Height);
                }
            }

            /// <summary>
            /// Get the range of children that are visible
            /// </summary>
            /// <param name="firstVisibleItemIndex">The item index of the first visible item</param>
            /// <param name="lastVisibleItemIndex">The item index of the last visible item</param>
            private void GetVisibleRange(out int firstVisibleItemIndex, out int lastVisibleItemIndex)
            {
                if (Orientation == Orientation.Horizontal)
                {
                    int childPerRow = CalculateChildrenPerRow(_extent);

                    firstVisibleItemIndex = (int)Math.Floor(_offset.Y / this.ItemSize.Height) * childPerRow;
                    lastVisibleItemIndex = (int)Math.Ceiling((_offset.Y + _viewport.Height) / this.ItemSize.Height) * childPerRow - 1;

                    ItemsControl itemsControl = ItemsControl.GetItemsOwner(this);
                    int itemCount = itemsControl.HasItems ? itemsControl.Items.Count : 0;
                    if (lastVisibleItemIndex >= itemCount)
                        lastVisibleItemIndex = itemCount - 1;
                }
                else
                {
                    int childPerCol = CalculateChildrenPerCol(_extent);

                    firstVisibleItemIndex = (int)Math.Floor(_offset.X / this.ItemSize.Width) * childPerCol;
                    lastVisibleItemIndex = (int)Math.Ceiling((_offset.X + _viewport.Width) / this.ItemSize.Width) * childPerCol - 1;

                    ItemsControl itemsControl = ItemsControl.GetItemsOwner(this);
                    int itemCount = itemsControl.HasItems ? itemsControl.Items.Count : 0;
                    if (lastVisibleItemIndex >= itemCount)
                        lastVisibleItemIndex = itemCount - 1;
                }

            }

            private Rect GetChildRect(int itemIndex, Size finalSize)
            {
                if (Orientation == Orientation.Horizontal)
                {
                    int childPerRow = CalculateChildrenPerRow(finalSize);

                    int row = itemIndex / childPerRow;
                    int column = itemIndex % childPerRow;

                    return new Rect(column * this.ItemSize.Width, row * this.ItemSize.Height, this.ItemSize.Width, this.ItemSize.Height);
                }
                else
                {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值