public int CalculateItemsPerRowCount(Size availableSize)
{
return 1;
}
/// <summary>
/// 计算行数
/// </summary>
/// <param name="availableSize"></param>
/// <returns></returns>
public int CalculateRowCount(Size availableSize)
{
return _virtualizingPanel.Items.Count;
}
/// <summary>
/// 计算滚动面积
/// </summary>
/// <param name="availableSize"></param>
/// <returns></returns>
public Size CalculateExtent(Size availableSize)
{
var height = GetItemHeight(availableSize);
var rowCount = CalculateRowCount(availableSize);
return new Size(availableSize.Width, height * rowCount);
}
/// <summary>
/// 计算可见区域内的Item范围
/// </summary>
/// <param name="availableSize"></param>
/// <returns></returns>
public ItemRange CalculateItemRange(Size availableSize)
{
if (!this._virtualizingPanel.IsVirtualizing)
{
return new ItemRange(0, this._virtualizingPanel.Items.Count - 1);
}
var viewportHeight = _virtualizingPanel.ViewportHeight;
var offsetY = _virtualizingPanel.VerticalOffset;
var rowCount = this.CalculateRowCount(availableSize);
var itemHeight = this.GetItemHeight(availableSize);
var firstVisibleItemIndex = (int)Math.Floor(offsetY / itemHeight);
var lastVisibleItemIndex = (int)Math.Ceiling((offsetY + viewportHeight) / itemHeight) - 1;
if (lastVisibleItemIndex >= rowCount)
lastVisibleItemIndex = rowCount - 1;
return new ItemRange(firstVisibleItemIndex, lastVisibleItemIndex);
}
/// <summary>
/// Item高度
/// </summary>
public double ItemHeight
{
get { return (double)GetValue(ItemHeightProperty); }
set { SetValue(ItemHeightProperty, value); }
}
public static readonly DependencyProperty ItemHeightProperty =
DependencyProperty.Register("ItemHeight", typeof(double), typeof(VirtualizingStackPanelBuilder), new PropertyMetadata(ItemHeightPropertyChangedCallback));
public static void ItemHeightPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)