2010-07-30 2 views
2

XAML에서만 데이터 바인딩을 사용하여 동적으로 정의 된 일반 그리드 행 및 열을 갖기 위해 노력하고 있습니다.XAML과 Silverlight 4를 사용하면 데이터 바인딩에서 동적 GRID 행과 열을 구축 할 수 있습니까?

나는이 코드를 사용할 수 있지만 전적으로 XAML에서이를 수행 할 방법을 찾고 있습니다.

의견이 있으십니까? 그것은 작동하지만 나는 그것이 최적의 솔루션입니다 확실하지 않다

public class DynamicGrid : Grid 
{ 
    public static readonly DependencyProperty NumColumnsProperty = 
     DependencyProperty.Register ("NumColumns", typeof (int), typeof (DynamicGrid), 
     new PropertyMetadata ((o, args) => ((DynamicGrid)o).RecreateGridCells())); 

    public int NumColumns 
    { 
     get { return (int)GetValue(NumColumnsProperty); } 
     set { SetValue (NumColumnsProperty, value); } 
    } 


    public static readonly DependencyProperty NumRowsProperty = 
     DependencyProperty.Register("NumRows", typeof(int), typeof(DynamicGrid), 
     new PropertyMetadata((o, args) => ((DynamicGrid)o).RecreateGridCells())); 

    public int NumRows 
    { 
     get { return (int)GetValue(NumRowsProperty); } 
     set { SetValue (NumRowsProperty, value); } 
    } 

    private void RecreateGridCells() 
    { 
     int numRows = NumRows; 
     int currentNumRows = RowDefinitions.Count; 

     while (numRows > currentNumRows) 
     { 
      RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); 
      currentNumRows++; 
     } 

     while (numRows < currentNumRows) 
     { 
      currentNumRows--; 
      RowDefinitions.RemoveAt(currentNumRows); 
     } 

     int numCols = NumColumns; 
     int currentNumCols = ColumnDefinitions.Count; 

     while (numCols > currentNumCols) 
     { 
      ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); 
      currentNumCols++; 
     } 

     while (numCols < currentNumCols) 
     { 
      currentNumCols--; 
      ColumnDefinitions.RemoveAt(currentNumCols); 
     } 

     UpdateLayout(); 
    } 
} 

:

+0

어쩌면 코드에서 어떻게 수행하고 있는지 게시 할 수 있습니다. 내 질문은 데이터 소스가 어떻게 생겼는지입니다. 열 이름에 대한 데이터 주석이 있습니까? 자동 생성 열은 어디에서 실패합니까? – itchi

답변

2

확인 후 많은 순 주위에 읽는 나는 다음과 같은 솔루션을 코딩. 이것에 대한 의견이 있으십니까?

관련 문제