Search code examples
silverlightdatagriddatagridtextcolumn

How do I detect when a cell's value has changed in Silverlight?


I'm working in Silverlight, trying to figure out how to set a grid cell font color based on the contents of the cell.

I have an ObservableCollection bound to a DataGrid, and my items implement INotifyPropertyChanged so the grid updates as I change the values; it's all working perfectly, including letting me sort items and keep the sorting while I update the underlying items.

I know I can use the LoadingRow event to change colors, but the only way I can get the event to fire is by changing the grids datasource, in which case my sorting goes out the window.

So, what I really want is a way to either

  1. loop the rows in the datagrid, find the cell I need, and change it's color or
  2. implement a custom column that I can use to dynamically set the color.

The problem is how to actually do either of those things :).


Solution

  • You should use databinding for this.

    1. Bind your cell font color to the content of the cell
    2. Create a converter IValueConverter that converts the value to a color depending from your needs

    See here for a good example
    http://weblogs.asp.net/joewrobel/archive/2009/01/25/conditional-formatting-in-the-silverlight-datagrid.aspx