How can we set the Wrap-point for the WrapPanel?

前端 未结 1 1698
执念已碎
执念已碎 2020-12-03 16:24

I have an ItemsControl and I want the data to be entered into two columns. When the User resizes to a width lesser than the second column, the items of the second column mus

相关标签:
1条回答
  • 2020-12-03 16:50

    There's an UniformWrapPanel article on codeproject.com, which I modified to ensure the items in a wrap panel have uniform width and fit to the width of the window. You should easily be able to modify this code to have a maximum number of columns. Try changing the code near

    var itemsPerRow = (int) (totalWidth/ItemWidth);
    

    in order to specify the max columns.

    Here's the code:

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题