locked
How to set background color for one cell in grid? RRS feed

  • Question

  •  If a Gird has more than row and column, I want to set one cell background color to a specific color, for example (3,5).

    How to implement this requirement?

    Tuesday, January 12, 2010 3:31 PM

Answers

  • Assuming LayoutRoot is the name of the Grid ,

    you can do something like this to find control in Row 2 Column 2

    LayoutRoot.Children.Where(x => x.GetValue(Grid.RowProperty).ToString() == "2" && x.GetValue(Grid.ColumnProperty).ToString() == "2")

     

    Tuesday, January 12, 2010 4:09 PM
  • Hi Kent.zhou,

     If a Gird has more than row and column, I want to set one cell background color to a specific color, for example (3,5).

    As far as I know, we cannot get the cell and set its background color directly.   As an alternative way, we can add StackPanel which fully covered the cell and then find the StackPanel, set its background color.  See this.

    var obj = LayoutRoot.Children.Where(element =>( Grid.GetRow(element as FrameworkElement).ToString() == "1" && Grid.GetColumn(element as FrameworkElement).ToString() == "1")).FirstOrDefault(); 
    

     

    Best regards,

    Jonathan

    Monday, January 18, 2010 10:30 PM

All replies

  • The below article should help. It contains an example on how to change the row background color, and the font size of a particular cell.

    http://leeontech.wordpress.com/2008/06/08/silverlight-datagrid-issues/
    Tuesday, January 12, 2010 3:38 PM
  •  Thanks. I am talking about Grid, not DataGrid.

    Tuesday, January 12, 2010 3:43 PM
  • Assuming LayoutRoot is the name of the Grid ,

    you can do something like this to find control in Row 2 Column 2

    LayoutRoot.Children.Where(x => x.GetValue(Grid.RowProperty).ToString() == "2" && x.GetValue(Grid.ColumnProperty).ToString() == "2")

     

    Tuesday, January 12, 2010 4:09 PM
  • Hi Kent.zhou,

     If a Gird has more than row and column, I want to set one cell background color to a specific color, for example (3,5).

    As far as I know, we cannot get the cell and set its background color directly.   As an alternative way, we can add StackPanel which fully covered the cell and then find the StackPanel, set its background color.  See this.

    var obj = LayoutRoot.Children.Where(element =>( Grid.GetRow(element as FrameworkElement).ToString() == "1" && Grid.GetColumn(element as FrameworkElement).ToString() == "1")).FirstOrDefault(); 
    

     

    Best regards,

    Jonathan

    Monday, January 18, 2010 10:30 PM