2011-10-02 89 views
0

所以我有一個垂直包裝的包裝紙。這些項目是在運行時添加的,但所有這些項目(用戶控件)具有不同的寬度,並且因爲wrappanel是垂直包裝的,所以它們將它們堆疊起來,當它們覆蓋垂直空間時,它們將包裝到下一列。但我需要的是「種類」雙向包裝,即我添加了第一個寬度爲200px的項目,然後我添加了第二個項目,寬度爲50px,但是當我添加第三個項目時,像100px寬度我希望它不會去到下一行,但將自己置於該自由點50px控件留在那裏取決於頂部的200px控件(留下150px空間和100px控件明顯適合)。當然,如果它不合適,它會捲到下一行,這一切都可以。用「雙面包裝」包裝面板

下面是一個圖像,以澄清這一點(不能在這裏upload'em):

這是發生了什麼: image 1

而這正是我想要的: image 2


對不起,我的英語,這不是我的主要語言。我希望你能理解我的問題。

回答

0

你絕對不能使用單個面板來完成!你可以使用一個堆疊面板在哪裏插入多個水平方向的動態wrappanel,以便有你需要的「列」行爲

+0

這是我必須用一個單一的面板做到這一點的問題:\我可以像Wrappanel-IN-A-wrappanel,但是,這並不爲工作我... 我在想一個自定義的包裝,但這就是我要求的;如何寫一個在我描述的行爲中起作用的人:? – vytaslll

0

嗯,我做到了。只是寫了一個自定義的包裝與我想要的行爲。

這就是:

public class TwoWayWrapPanel : Panel 
{ 
int _rowCount = 0; 

public int RowCount 
{ 
    get { return _rowCount; } 
    set { _rowCount = value; } 
} 

protected override Size MeasureOverride(Size availableSize) 
{ 
    Size resultSize = new Size(0, 0); 
    double columnWidth = 0; 
    double usedSpace = 0; 
    double nullX = 0; 
    double currentX = 0; 
    double currentY = 0; 
    bool isFirst = true; 
    int row = 0; 

    foreach (UIElement child in Children) 
    { 
     child.Measure(availableSize); 

     if (isFirst) 
     { 
      columnWidth = child.DesiredSize.Width; 
      resultSize.Width += columnWidth; 
      currentY += child.DesiredSize.Height; 
      row++; 
      isFirst = false; 
     } 
     else 
     { 
      if (columnWidth >= usedSpace + child.DesiredSize.Width & _rowCount > 1) 
      { 
       currentX = nullX + usedSpace; 
       usedSpace += child.DesiredSize.Width; 
      } 
      else 
      { 
       row++; 

       if (row + 1 > _rowCount | child.DesiredSize.Width > columnWidth) 
       { 
        row = 0; 
        currentX = nullX + columnWidth; 
        nullX = currentX; 
        usedSpace = 0; 
        columnWidth = child.DesiredSize.Width; 
        currentY = child.DesiredSize.Height; 
        row++; 
        resultSize.Width += columnWidth; 
       } 
       else 
       { 
        currentY += child.DesiredSize.Height; 
        currentX = nullX; 
        usedSpace = child.DesiredSize.Width; 
       } 
      } 
     } 
    } 

    return resultSize; 
} 

protected override Size ArrangeOverride(Size finalSize) 
{ 
    double columnWidth = 0; 
    double usedSpace = 0; 
    double nullX = 0; 
    double currentX = 0; 
    double currentY = 0; 
    bool isFirst = true; 
    int row = 0; 

    foreach (UIElement child in Children) 
    { 
     //First item in the collection 
     if (isFirst) 
     { 
      child.Arrange(new Rect(currentX, currentY, child.DesiredSize.Width, child.DesiredSize.Height)); 
      columnWidth = child.DesiredSize.Width; 
      currentY += child.DesiredSize.Height; 
      row++; 
      isFirst = false; 
     } 
     else 
     { 
      //Current item fits so place it in the same row 
      if (columnWidth >= usedSpace + child.DesiredSize.Width & _rowCount > 1) 
      { 
       currentX = nullX + usedSpace; 
       child.Arrange(new Rect(currentX, currentY, child.DesiredSize.Width, child.DesiredSize.Height)); 
       usedSpace += child.DesiredSize.Width; 
      } 
      else 
      { 
       row++; 

       //The row limit is reached or the item width is greater than primary item width. Creating new column 
       if (row + 1 > _rowCount | child.DesiredSize.Width > columnWidth) 
       { 
        row = 0; 
        currentY = 0; 
        currentX = nullX + columnWidth; 
        nullX = currentX; 
        usedSpace = 0; 
        child.Arrange(new Rect(currentX, currentY, child.DesiredSize.Width, child.DesiredSize.Height)); 
        columnWidth = child.DesiredSize.Width; 
        currentY += child.DesiredSize.Height; 
        row++; 
       } 
       //Item doesn't fit. Adding to the new row in the same column 
       else 
       { 
        usedSpace = 0; 
        currentY += child.DesiredSize.Height; 
        currentX = nullX; 
        child.Arrange(new Rect(currentX, currentY, child.DesiredSize.Width, child.DesiredSize.Height)); 
        usedSpace += child.DesiredSize.Width; 
       } 
      } 
     } 
    } 

    return finalSize; 
} 
}