【问题标题】:Silverlight - How do I make each item in a list box be the same height?Silverlight - 如何使列表框中的每个项目具有相同的高度?
【发布时间】:2009-07-23 16:09:56
【问题描述】:

如何使 silverlight 列表框的所有项目都具有相同的大小,并让它们占据列表框高度的 100%。 IE。 1 项将是列表框的高度,2 项将是列表框高度的 50%,等等...

编辑 - 这是代码

public class UniformPanel : Panel
{
    protected override Size MeasureOverride(Size availableSize)
    {
        Size panelDesiredSize = new Size();

        for (int i = 0; i < Children.Count; i++)
        {
            UIElement child = Children[i];
            child.Measure(availableSize);
            var childDesiredSize = child.DesiredSize;
            panelDesiredSize.Height += childDesiredSize.Height;
            if (panelDesiredSize.Width < childDesiredSize.Width)
            {
                panelDesiredSize.Width = childDesiredSize.Width;
            }
        }

        return panelDesiredSize;
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        double height = finalSize.Height/Children.Count;
        for (int i = 0; i < Children.Count; i++)
        {
            UIElement child = Children[i];
            Size size = new Size(finalSize.Width, height);
            child.Arrange(new Rect(new Point(0, i * height), size));
        }
        return finalSize; // Returns the final Arranged size
    }
}

【问题讨论】:

    标签: c# .net wpf silverlight


    【解决方案1】:

    我明白了,您的要求只是让每个项目都获得正确的 TotalHeight/itemsCount。在这种情况下,您可以通过使 Columns=1 来使用 UniformGrid(它已经作为平台的一部分)来解决问题 使用以下 XAML

            <ListBox VerticalContentAlignment="Stretch"  HorizontalContentAlignment="Stretch">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid IsItemsHost="True" Columns="1"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
              <Button />
              <Button />
              <Button />
        </ListBox>
    

    更新:silverlight 没有 UniformGrid,但你可以得到一个我从 WPF 移植到 Silverlight here

    【讨论】:

      【解决方案2】:

      我不认为这将是微不足道的,因为列表框有无限的高度,添加足够的项目,滚动条可以显示并且内容的高度会增加。

      列表框的 rendersize.height 将让您确定可用高度是多少,然后告诉子项调整大小,但 Jobi 是正确的,因为您最好将所有这些都封装在自定义面板中。子项也需要接受调整大小,而不是使用无法覆盖的固定高度。

      理论上,每个孩子都可以检查父母渲染的高度和项目计数,然后给它一个渲染高度的百分比,它可以使用并适当调整大小,但会想到混乱。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-13
        • 1970-01-01
        相关资源
        最近更新 更多