Tip:Changing Foreground on row selection in Datagrid

Posted on January 6, 2009. Filed under: Silverlight |

When we want to change the foreground of the cells in datagrid when the row is selected it seems easier to write a few lines of code than messing with the templates
In the selectionchanged handler, we can loop throgh the columns and set the foreground of each cell like below

void datagrid2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (DataGridColumn col in datagrid2.Columns)
{
FrameworkElement ele = col.GetCellContent(e.AddedItems[0]);
(ele as TextBlock).Foreground = new SolidColorBrush(Colors.Blue);
if (e.RemovedItems.Count > 0)
{
ele = col.GetCellContent(e.RemovedItems[0]);
(ele as TextBlock).Foreground = new SolidColorBrush(Colors.Black);
}
}
}

Make a Comment

Make A Comment: ( 4 so far )

blockquote and a tags work here.

4 Responses to “Tip:Changing Foreground on row selection in Datagrid”

RSS Feed for Lee’s corner Comments RSS Feed

Nice on Lee.

Should be…

Nice one Lee.

Thanks.

Its not that hard to do it with templates.
You can wrap the DataGridCellPresenter into a ContentControl and modify its properties.

Here is how:

http://alejandrobog.wordpress.com/2009/07/02/silverlight-datagrid-change-foreground-color-on-visualstatemanagermouseover-selected/


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...