0

I implemented custom Windows.UI.Xaml.Controls.Panel for custom layout items in ListView and put it to <ItemsPanelTemplate>. It's works, but is not possible to scroll. This answer not applicable because I'm using MVVM data binding. How to make scrolling work?

My Panel implementation:

public class StaggeredWrapPanel : Panel
{
   #region int ItemWidth Property

    public int ItemWidth
    {
        get { return (int)GetValue(ItemWidthProperty); }
        set { SetValue(ItemWidthProperty, value); }
    }

    public static readonly DependencyProperty ItemWidthProperty = DependencyProperty.Register("ItemWidth", typeof(int), typeof(StaggeredWrapPanel), new PropertyMetadata(300, OnItemWidthPropertyChanged));

    private static void OnItemWidthPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        (source as StaggeredWrapPanel).InvalidateMeasure();
    }
    #endregion

    protected override Size MeasureOverride(Size availableSize)
    {
        foreach (var item in Children)
        {
            item.Measure(new Size(ItemWidth, double.PositiveInfinity));
        }
        return base.MeasureOverride(availableSize);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        int columnsCount = (int)(finalSize.Width / ItemWidth);
        double[] columnsOffsetsY = new double[columnsCount];
        double[] columnsOffsetsX = new double[columnsCount];
        for (int i = 0; i < columnsOffsetsX.Length; i++)
        {
            columnsOffsetsX[i] = i * ItemWidth;
        }
        int currentColumn = 0;
        foreach (var item in Children)
        {
            if (currentColumn != 0 && columnsOffsetsY[currentColumn] >= columnsOffsetsY[currentColumn - 1])
                if (currentColumn == columnsCount - 1)
                    currentColumn = 0;
                else currentColumn++;
            Rect rect = new Rect(new Point(columnsOffsetsX[currentColumn], columnsOffsetsY[currentColumn]), new Size(item.DesiredSize.Width, item.DesiredSize.Height));
            columnsOffsetsY[currentColumn] += item.DesiredSize.Height;
            item.Arrange(rect);
            if (currentColumn == 0)
                currentColumn++;
            if (currentColumn > columnsCount - 1)
                currentColumn = 0;
        }
        return base.ArrangeOverride(finalSize);
    }

}
SUDALV
  • 91
  • 9
  • The scrolling should be taken care by the ScrollViewer inside the ListView. There must be something wrong with your panel implementation - the height of your panel somehow doesn't go over the size of the screen. You need to show the code. – Justin XL Jan 09 '15 at 23:49
  • @justin-xl ok, I'm published code of the Panel. – SUDALV Jan 12 '15 at 08:50
  • Do you expect something like this http://stackoverflow.com/questions/14084507/implementing-a-multicolumn-listview-with-independant-row-heights? I know it's Android but I just want to make sure that design is what you are looking for. A grid which you can specify number of columns on each row with dynamic heights. – Justin XL Jan 12 '15 at 09:45
  • @JustinXL yes, but another problem found: impossible implement VirtualizingPanel (need for "endless" scrolling). – SUDALV Jan 13 '15 at 13:44

0 Answers0