2014-01-06 2 views
0

WP8.SDk 용 테이블을 만들어야합니다.동적으로 생성 된 격자의 행 배경 설정

저는이 목적으로 그리드를 사용하고 있습니다. 행과 열의 동적 그리드를 만들었습니다.

는 지금은

내 C# 코드가 여기에있다 .. 헤더 다른 만들기 위해 배경 이미지를 설정합니다. 이 같은

private void table_click(object sender, RoutedEventArgs e) 
{ 
    //Creation of Grid 
    Grid tablegrid = new Grid(); 
    tablegrid.Height = double.NaN; 
    tablegrid.Width = 454; 
    tablegrid.Margin = new Thickness(0, 66, 0, 0); 
    tablegrid.ShowGridLines = true; 
    tablegrid.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
    tablegrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 

    //Table Rows and Columns Definition 
    string[] tablerow = new string[] { "Submit Report", "Something", "New" }; 
    string[] tablecol = new string[] { "Mansab", "Arsal", "Ali" }; 

    int i, j; 
    for (i = 0; i < tablerow.Length; i++) 
    { 
     RowDefinition gridrows = new RowDefinition(); 
     gridrows.Height = new GridLength(66); 

     if (i == 0) 
     { 
      var brush = new ImageBrush(); 
      brush.ImageSource = new BitmapImage(new Uri("Resources/Images/top_bar_bg.png", UriKind.Relative)); 

      //gridrows.Background = brush; //This not works 
     } 

     tablegrid.RowDefinitions.Insert(i, gridrows); 
    } 
    for (j = 0; j < tablecol.Length; j++) 
    { 
     ColumnDefinition gridCol = new ColumnDefinition(); 
     tablegrid.ColumnDefinitions.Insert(j, gridCol); 
    } 
    tablegrid.ShowGridLines = true; 
    ContentPanel.Children.Add(tablegrid); 
} 

내 테이블의 모양을

http://www.4shared.com/download/SlNX8mbpba/Table.PNG 

하지만 난이 이런 식으로 뭔가를 만들기 위해 :

http://www.4shared.com/download/j-qT-kGVce/table2.PNG 

어떻게 동적으로 각 행의 배경을 지정할 수 있습니다?

답변

0

이렇게하면이 문제가 해결되었습니다.

private void table_click(object sender, RoutedEventArgs e) 
    { 
     //Creation of Grid 
     Grid tablegrid = new Grid(); 
     tablegrid.Height = double.NaN; 
     tablegrid.Width = double.NaN; 
     tablegrid.Margin = new Thickness(0, 66, 0, 0); 
     // tablegrid.ShowGridLines = true; 
     tablegrid.VerticalAlignment = System.Windows.VerticalAlignment.Top; 
     tablegrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 


     //Table Rows and Columns Definition 
     string[] tablerow = new string[] { "Submit Report","Arsal","AA","Mansab","a","e"}; 
     string[] tablecol = new string[] { "Mansab","Ali","Aly","Ayaz" }; 


     int i, j; 
     for (i = 0; i < tablerow.Length; i++) 
     { 
      RowDefinition gridrows = new RowDefinition(); 
      gridrows.Height = new GridLength(66); 
      tablegrid.RowDefinitions.Insert(i, gridrows); 
     } 
     for (j = 0; j < tablecol.Length; j++) 
     { 
      ColumnDefinition gridCol = new ColumnDefinition(); 
      tablegrid.ColumnDefinitions.Insert(j, gridCol); 
     } 

    // Setting background Image for all rows and columns Dynamically 

     int k = 0; 
     for (k = 0; k < tablecol.Length; k++) 
     { 
      Border brd = new Border(); 
      brd.Height = 66; 
      brd.Width = 460; 
      brd.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
      Grid.SetRow(brd, 0); 
      Grid.SetColumnSpan(brd, tablecol.Length); 
      tablegrid.Children.Add(brd); 
      var brush = new ImageBrush(); 
      brush.ImageSource = new BitmapImage(new Uri("Resources/Images/top_bar_bg.png", UriKind.Relative)); 
      brd.Background = brush; 
     } 
     int l; 
     for (l = 0; l < tablecol.Length; l++) 
     { 
      Border brd = new Border(); 
      brd.Height = 66; 
      brd.Width = 460; 
      brd.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; 
      Grid.SetRow(brd, 1); 
      Grid.SetColumnSpan(brd, tablecol.Length); 
      tablegrid.Children.Add(brd); 
      var brush = new ImageBrush(); 
      brush.ImageSource = new BitmapImage(new Uri("Resources/Images/table_row_blue.png", UriKind.Relative)); 
      brd.Background = brush; 
     } 
     ContentPanel.Children.Add(tablegrid); 
    } 
1

ColumnHeaderStyle 속성을 사용하여 열 머리글을 콘텐츠 행과 다르게 설정할 수 있습니다. 매번 각 행의 BG 색상을 설정할 필요성을 지정할 수 있습니까? 특별한 필요가 없다면 DataGrid의 AlternateRowStyle 속성을 사용할 수 있습니다.

+0

Windows Phone 8 SDK에 대해 DataGrid를 사용할 수 없습니다. 여기 그리드입니다. 이걸 테이블로 사용하려고합니다. 헤더에 대한 배경 이미지가 필요합니다. 헤더에 대해이 작업을 수행 할 수 있다면 모든 행과 열을 넣을 수 있습니다 ..하지만 여기서는 제가 할 수 없습니다 .. 그래서이 문제를 해결할 수 있습니까? – Arsal

1

WPF에서는 Grid에 '셀'이라는 개념이 없으므로 '셀'의 배경을 색칠 할 수 없습니다. 이 대신에 셀에 무언가를 넣고 그 컨트롤의 배경을 설정할 수 있습니다. 예를 들어, 'header'텍스트의 TextBlock.Background 속성을 색칠 할 수 있습니다.

또는 StackPanel 또는 그와 비슷한 것을 셀에 넣고 TextBlock을 넣은 다음 StackPanel.Background 속성을 지정할 수 있습니다.


UPDATE >>> 개인적으로

, 나는 당신이 당신의 요구 사항을 구현하려고하는 밥맛 방법을 포착했다고 말하고 싶지만. 제 생각에는 헤더 행에 Grid을 사용하고 ItemsTemplateListBox을 사용하고 데이터 행에 또 다른 Grid을 포함하는 것이 훨씬 더 간편하다고 생각합니다. Grid.IsSharedSizeScope Attached Property을 사용하여 열을 서로 나란히 놓을 수 있습니다.

현재 '이미지'문제인 경우 위에 Image을 표시 한 다음 머리글을 Grid 위에 겹칠 수도 있습니다.

+0

네가 맞다고 생각합니다 .... 나는 TextBlock을 사용하고 배경색을 설정했습니다. 하지만 모든 머리글 행에 이미지를 넣어야합니다 ... 나는 TextBlock을 추가하여 각 셀에 그림을 추가 할 수 없습니다. 이것은 끔찍한 일입니다. – Arsal

관련 문제