2011-10-02 4 views
0

그래서 세로로 감싸는 wrappanel 있습니다. 항목은 런타임에 추가되지만 모든 항목 (사용자 정의 컨트롤)의 너비가 다르므로 래퍼 vertically이 세로로 감싸 져 있기 때문에 겹쳐서 표시되며 세로 공간을 덮으면 다음 열로 감 쌉니다. 하지만 내가 필요로하는 것은 양방향 배치의 "종류"입니다. 즉 너비가 200px 인 첫 번째 항목을 추가 한 다음 너비가 50px 인 두 번째 항목을 추가했지만 100px와 같은 세 번째 항목을 추가하면 너는 다음 열로 가지 않길 바래. 그러나 그 자리에 200px 컨트롤에 따라 50px 컨트롤이 남아있다. (150px 스페이스와 100px 컨트롤이 잘 맞는다.) 물론 그것이 맞지 않을 때, 그것은 다음 줄로 감싸고, 그것은 모두 괜찮습니다."일종의"양방향 wraping 패널

여기이를 명확히하는 이미지이다 (여기 upload'em 수 없습니다) :

무슨 일이야 그 : image 1

그리고 내가 원하는 무엇 : image 2


나의 영어를 유감스럽게 생각하며, 그것은 제 1 차 언어가 아닙니다. 내 질문을 이해하길 바래.

답변

0

당신은 분명히 그것을 달성하기 위해 하나의 패널을 사용할 수 없습니다! 스택 패널을 사용하여 가로 방향으로 여러 개의 동적 래퍼 넬을 삽입하여 필요한 "열"동작을 수행 할 수 있습니다.

+0

나 ... 커스텀 래퍼 넬에 대해서 생각하고 있었는데, 그게 내가 원하는거야. 내가 기술 한 행동에서 작동하는 것을 쓰는 방법 :? – vytaslll

0

글쎄, 해냈어. 내가 원하는 행동으로 커스텀 래퍼 넬을 썼다. 여기

그 것이다 : 나는 Wrappanel - 인 - 어 - wrappanel 같은 것을 할 수있다 \하지만이 작동하지 않습니다 : 나는 그렇게 하나의 패널을 사용해야하는 문제가있어

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; 
} 
}