C# 设置包裹面板中的最大行数

C# 设置包裹面板中的最大行数,c#,wpf,xaml,panel,wrappanel,C#,Wpf,Xaml,Panel,Wrappanel,我需要配置一个包装面板,我可以在其中设置最大行数或最大列数 这是非常必要的,我使用的是WPF4.0。但有一天,我正在编写Metro应用程序,我记得它的一个控件有这个属性,但在WPF中没有(直到我知道) WPF 4.0中是否存在此类控制?或者我需要创建一个新的网格吗?我认为对于您试图实现的目标,WPF网格是更好的解决方案。 通过设置特定的网格行数,您可以模拟包裹面板的行为,这样更灵活 <Grid> <Grid.ColumnDefinitions> <Col

我需要配置一个包装面板,我可以在其中设置最大行数或最大列数

这是非常必要的,我使用的是WPF4.0。但有一天,我正在编写Metro应用程序,我记得它的一个控件有这个属性,但在WPF中没有(直到我知道)


WPF 4.0中是否存在此类控制?或者我需要创建一个新的网格吗?

我认为对于您试图实现的目标,WPF网格是更好的解决方案。 通过设置特定的网格行数,您可以模拟包裹面板的行为,这样更灵活

<Grid>
  <Grid.ColumnDefinitions>
    <ColumDefinition Height="Auto"/>
    <ColumDefinition Height="Auto"/>
    <ColumDefinition Height="Auto"/>
    .... 
    <ColumDefinition Height="*/>  
  </Grid.ColumnDefinitions>
<Grid>

.... 

您可以设置
ItemHeight
ItemWidth
属性来设置最大行数和列数


有关更多信息,请看一看

我认为使用WrapPanel可以做到这一点的唯一方法是,如果您知道对象的大小(并且它们是一致的),那么您可以相应地设置WrapPanel的高度/宽度。不过这很难看


需要考虑的一件事是:对于超出最大行/列数的元素,您希望面板做什么?还是元素的数量总是正确的?如果是这样的话,那么你应该真正地看看网格。

这里是这样一个包装的实现

Xaml:

<loc:WrapPanelWithRowsOrColumnsCount
    xmlns:loc="clr-namespace:..."
    Orientation="Vertical"
    RowsOrColumnsCount="2">
    <TextBox Text="Andrew" Margin="2" Height="30" />
    <TextBox Text="Betty" Margin="2" Height="40" />
    <TextBox Text="Celine" Margin="2" Height="20" />
    <TextBox Text="Dick" Margin="2" Height="20" />
    <TextBox Text="Enron" Margin="2" Height="30" />
    <TextBox Text="Felix" Margin="2" Height="20" />
    <TextBox Text="Hanibal" Margin="2" Height="30" />
</loc:WrapPanelWithRowsOrColumnsCount>

该解决方案的灵感来自。

如果添加了一个元素,并且达到了最大行数(例如行数),我们需要一个新行来显示该元素,那么会发生什么情况?也许您可以使用UniformGrid?e、 g.
我复制了代码,但它对包装面板中的项目(如@nikotromus)也没有影响
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

public class WrapPanelWithRowsOrColumnsCount : WrapPanel
{
    public static readonly DependencyProperty RowsOrColumnsCountProperty = 
        DependencyProperty.Register(
            "RowsOrColumnsCount",
            typeof(int),
            typeof(WrapPanelWithRowsOrColumnsCount),
            new PropertyMetadata(int.MaxValue));

    public int RowsOrColumnsCount
    {
        get { return (int)GetValue(RowsOrColumnsCountProperty); }
        set { SetValue(RowsOrColumnsCountProperty, Math.Max(value, 1)); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        if (Children.Count > 0)
        {
            Size newAvailableSize;

            if (Orientation == Orientation.Horizontal)
            {
                var suitableWidth = EstimateSuitableRowOrColumnLength(Children.Cast<UIElement>(),
                                                                        true,
                                                                        availableSize,
                                                                        RowsOrColumnsCount);

                newAvailableSize = 
                    double.IsNaN(suitableWidth) || suitableWidth <= 0
                        ? availableSize 
                        : new Size(Math.Min(suitableWidth, availableSize.Width), availableSize.Height);
            }
            else
            {
                var suitableHeigth = EstimateSuitableRowOrColumnLength(Children.Cast<UIElement>(),
                                                                        false,
                                                                        availableSize,
                                                                        RowsOrColumnsCount);
                newAvailableSize =
                    double.IsNaN(suitableHeigth) || suitableHeigth <= 0
                        ? availableSize
                        : new Size(availableSize.Width, Math.Min(suitableHeigth, availableSize.Height));
            }

            return base.MeasureOverride(newAvailableSize);
        }
        else
        {
            return base.MeasureOverride(availableSize);
        }
    }

    private double EstimateSuitableRowOrColumnLength(IEnumerable<UIElement> elements,
                                                        bool trueRowsFalseColumns,
                                                        Size availableSize,
                                                        int rowsOrColumnsCount)
    {
        var elementsList = elements.ToList();

        var desiredLengths = elementsList.Select(el => DesiredLength(el, availableSize, trueRowsFalseColumns)).ToList();

        var maxLength = desiredLengths.Where(length => !double.IsNaN(length)).Concat(new[] { 0.0 }).Max();

        if (maxLength <= 0.0)
        {
            return double.NaN;
        }

        var desiredLengthsRepaired = desiredLengths.Select(length => double.IsNaN(length) ? maxLength : length).ToList();

        var totalDesiredLength = desiredLengthsRepaired.Sum();

        var maxCount = Math.Min(rowsOrColumnsCount, elementsList.Count);

        var suitableRowOrColumnLength = totalDesiredLength / maxCount;

        double nextLengthIncrement;

        while (CountRowsOrColumnsNumber(desiredLengthsRepaired, suitableRowOrColumnLength, out nextLengthIncrement) > maxCount)
        {
            suitableRowOrColumnLength += nextLengthIncrement;
        }

        suitableRowOrColumnLength = Math.Max(suitableRowOrColumnLength, desiredLengthsRepaired.Max());

        return suitableRowOrColumnLength;
    }

    private int CountRowsOrColumnsNumber(List<double> desiredLengths, double rowOrColumnLengthLimit, out double nextLengthIncrement)
    {
        int rowOrColumnCount = 1;
        double currentCumulativeLength = 0;
        bool nextNewRowOrColumn = false;

        var minimalIncrement = double.MaxValue;

        foreach (var desiredLength in desiredLengths)
        {
            if (nextNewRowOrColumn)
            {
                rowOrColumnCount++;
                currentCumulativeLength = 0;
                nextNewRowOrColumn = false;
            }

            if (currentCumulativeLength + desiredLength > rowOrColumnLengthLimit)
            {
                minimalIncrement = Math.Min(minimalIncrement,
                                            currentCumulativeLength + desiredLength - rowOrColumnLengthLimit);

                if (currentCumulativeLength == 0)
                {
                    nextNewRowOrColumn = true;
                    currentCumulativeLength = 0;
                }
                else
                {
                    rowOrColumnCount++;
                    currentCumulativeLength = desiredLength;
                }
            }
            else
            {
                currentCumulativeLength += desiredLength;
            }
        }

        nextLengthIncrement = minimalIncrement != double.MaxValue ? minimalIncrement : 1;

        return rowOrColumnCount;
    }

    private double DesiredLength(UIElement el, Size availableSize, bool trueRowsFalseColumns)
    {
        el.Measure(availableSize);
        Size next = el.DesiredSize;

        var length = trueRowsFalseColumns ? next.Width : next.Height;

        if (Double.IsInfinity(length) ||
            Double.IsNaN(length))
        {
            return Double.NaN;
        }
        else
        {
            return length;
        }
    }
}