我们如何设置WrapPanel的Wrap-point?

时间:2012-03-19 11:49:58

标签: wpf wrapping wrappanel uniformgrid

我有一个ItemsControl,我希望将数据输入两列。当用户调整宽度小于第二列的宽度时,第二列的项必须包装到第一列。类似于UniformGrid但有包装的东西。

我设法使用WrapPanel来执行此操作。但我被迫操纵并硬编码WrapPanel的ItemWidth和MaxWidth来实现两列事物和包装。这不是一个好习惯。

无论如何设置最大列数或者换句话说允许我们决定WrapPanel应该在什么时候开始包装?

一些在互联网上浏览的消息显示,Windows 8 Metro中的WrapGrid有property。有人在WPF中有这个实现吗?

1 个答案:

答案 0 :(得分:4)

在codeproject.com上有一篇UniformWrapPanel文章,我修改了这篇文章以确保包装面板中的项目具有统一的宽度并适合窗口的宽度。您应该可以轻松地修改此代码以获得最大列数。尝试更改

附近的代码
var itemsPerRow = (int) (totalWidth/ItemWidth);

以指定最大列。

以下是代码:

public enum ItemSize
{
    None,
    Uniform,
    UniformStretchToFit
}

public class UniformWrapPanel : WrapPanel
{
    public static readonly DependencyProperty ItemSizeProperty =
        DependencyProperty.Register(
            "ItemSize", 
            typeof (ItemSize), 
            typeof (UniformWrapPanel), 
            new FrameworkPropertyMetadata(
                default(ItemSize),
                FrameworkPropertyMetadataOptions.AffectsMeasure,
                ItemSizeChanged));

    private static void ItemSizeChanged(
        DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var uniformWrapPanel = sender as UniformWrapPanel;
        if (uniformWrapPanel != null)
        {
            if (uniformWrapPanel.Orientation == Orientation.Horizontal)
            {
                uniformWrapPanel.ItemWidth = double.NaN;
            }
            else
            {
                uniformWrapPanel.ItemHeight = double.NaN;
            }
        }
    }

    public ItemSize ItemSize
    {
        get { return (ItemSize) GetValue(ItemSizeProperty); }
        set { SetValue(ItemSizeProperty, value); }
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        var mode = ItemSize;

        if (Children.Count > 0 && mode != ItemSize.None)
        {
            bool stretchToFit = mode == ItemSize.UniformStretchToFit;

            if (Orientation == Orientation.Horizontal)
            {
                double totalWidth = availableSize.Width;

                ItemWidth = 0.0;
                foreach (UIElement el in Children)
                {
                    el.Measure(availableSize);
                    Size next = el.DesiredSize;
                    if (!(Double.IsInfinity(next.Width) || Double.IsNaN(next.Width)))
                    {
                        ItemWidth = Math.Max(next.Width, ItemWidth);
                    }
                }

                if (stretchToFit)
                {
                    if (!double.IsNaN(ItemWidth) && !double.IsInfinity(ItemWidth) && ItemWidth > 0)
                    {
                        var itemsPerRow = (int) (totalWidth/ItemWidth);
                        if (itemsPerRow > 0)
                        {
                            ItemWidth = totalWidth/itemsPerRow;
                        }
                    }
                }
            }
            else
            {
                double totalHeight = availableSize.Height;

                ItemHeight = 0.0;
                foreach (UIElement el in Children)
                {
                    el.Measure(availableSize);
                    Size next = el.DesiredSize;
                    if (!(Double.IsInfinity(next.Height) || Double.IsNaN(next.Height)))
                    {
                        ItemHeight = Math.Max(next.Height, ItemHeight);
                    }
                }

                if (stretchToFit)
                {
                    if (!double.IsNaN(ItemHeight) && !double.IsInfinity(ItemHeight) && ItemHeight > 0)
                    {
                        var itemsPerColumn = (int) (totalHeight/ItemHeight);
                        if (itemsPerColumn > 0)
                        {
                            ItemHeight = totalHeight/itemsPerColumn;
                        }
                    }
                }
            }
        }

        return base.MeasureOverride(availableSize);
    }
}