Border disappearing on ItemsControl resize (using “stretchpanel”)

后端 未结 1 1111
谎友^
谎友^ 2021-01-23 09:43

Yesterday I asked how to get an ItemsControl to have its children equally distributed across the available space; and after happily reading the answer, I wrote (well actually ki

相关标签:
1条回答
  • 2021-01-23 10:33

    It will work if you write your StretchPanel like this, which measure its child elements with zero width and height:

    public class StretchPanel : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            foreach (UIElement element in Children)
            {
                element.Measure(new Size());
            }
    
            return new Size();
        }
    
        protected override Size ArrangeOverride(Size finalSize)
        {
            for (int i = 0; i < Children.Count; i++)
            {
                int x1 = (int)(finalSize.Width / Children.Count * i);
                int x2 = (int)(finalSize.Width / Children.Count * (i + 1));
                Children[i].Arrange(new Rect(x1, 0, x2 - x1, finalSize.Height));
            }
    
            return finalSize;
        }
    }
    

    and then set HorizontalAlignment="Stretch" (or don't set it at all, since that is the default value) on the ItemsControl.

    0 讨论(0)
提交回复
热议问题