2014-01-06 32 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沒有「單元」的概念,所以你不能着色「單元」的背景。取而代之的是,您可以將某些東西放入您的單元格中並設置該控件的背景。例如,您可以爲「標題」文本的TextBlock.Background屬性着色。

或者,您可以將StackPanel或類似物放入單元格中,然後將TextBlock置於該單元格內,然後爲StackPanel.Background屬性着色。


UPDATE >>>

就個人而言,我會說,你選擇了一個粗糙的方法來嘗試實現您的要求。我認爲你會發現僅僅將Grid用於標題行,將ListBoxItemsTemplate包含用於數據行的另一Grid相比更容易。您可以使用Grid.IsSharedSizeScope Attached Property使列保持一致。

對於你目前的'形象'問題,你甚至可以在ListBox上面顯示Image,然後在其上覆蓋標題Grid

+0

是的,你是對的,我想....我用TextBlock做了它,並設置背景顏色。但是我必須在所有標題行中放置圖像...我不能通過添加TextBlock來在每個單元格中添加圖片。這將會很可怕。 – Arsal