2

我正在尝试拉伸包装面板中的内容,而不会失去其多行功能。我的意图是,如果内容小于屏幕的宽度,那么它将拉伸任何未定义的内容以进行填充。这可能吗?有没有在其他地方做过?

到目前为止我写的代码:

    protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
    {
        if (this.Children.Count == 0)
            return base.ArrangeOverride(finalSize);
        if (FlexibleColumn == null)
            return base.ArrangeOverride(finalSize);

        double totalWidth = 0;

        for (int i = 0; i > Children.Count; i++)
        {
            if (i == FlexibleColumn)
                continue;
            totalWidth = totalWidth + Children[i].DesiredSize.Width;
        }

        if (totalWidth < finalSize.Width)
        {
            Children[FlexibleColumn].DesiredSize.Width = finalSize.Width - totalWidth;
        }

        return base.ArrangeOverride(finalSize);
    }

然而,Children 仅用于获取,而非设置。任何其他方式来操纵它作为这一点?

谢谢

4

1 回答 1

0

有许多自定义包装面板:(http://www.codeproject.com/Articles/32629/A-better-panel-for-data-binding-to-a-WrapPanel-in

基本上你必须覆盖方法 MeasureOverride

提示是设置 WrapPanel 中存在的 ItemWidth 属性。例如:

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

    double totalWidth = availableSize.Width;

    if (!double.IsNaN(supostWidth) && !double.IsInfinity(supostWidth) && supostWidth > 0)
    {
        var itemsPerRow = (int)(totalWidth / supostWidth);
        if (itemsPerRow > 0)
        {
            ItemWidth = totalWidth / itemsPerRow;
        }
    }
}
于 2012-09-22T17:37:49.847 回答