Skip to main content Skip to footer

C1DataGrid Cell Animation Part 2

We saw how we can animate cells to show value changes in the WPF version of C1DataGrid here. What if we want to achieve the same in the Silverlight version of C1DataGrid? Silverlight binding does not expose the 'NotifyOnTargetUpdated' property and the binding class does not have the 'SourceUpdated' event, so that makes the scene a bit tricky. The answer is to use the bound data item's 'PropertyChanged' event and animate the cells there. Needless to say, the bound object class must implement 'INotifyPropertyChanged' interface. The good news is that the method is pretty simple and is applicable for both WPF/Silverlight. Now, the trick here is to listen to the 'LoadedCellPresenter' event of the C1DataGrid and add event handler to data item PropertyChanged event. We will be animating the cell presenter in same PropertyChanged event.


c1datagrid.LoadedCellPresenter += (s, e) =>  
{  
    if (e.Cell.Presenter.Content is TextBlock)  
    {  
        ((e.Cell.Presenter.Content as TextBlock).GetBindingExpression(TextBlock.TextProperty).DataItem as MyDataItem).PropertyChanged += (s1, e1) =>  
        {  
            if (e1.PropertyName == (e.Cell.Column as C1.Silverlight.DataGrid.DataGridBoundColumn).Binding.Path.Path)  
            {  
                var ca = new ColorAnimation()  
                {  
                    From = Colors.Transparent,  
                    To = Colors.Red,  
                    Duration = TimeSpan.FromSeconds(1)  
                };  
                Storyboard.SetTarget(ca, e.Cell.Presenter);  
                Storyboard.SetTargetProperty(ca, new PropertyPath("(Background).(SolidColorBrush.Color)"));  
                Storyboard sb = new Storyboard  
                {  
                    Duration = TimeSpan.FromSeconds(1),  
                    Children = { ca },  
                    AutoReverse = true,  
                };  
                sb.Begin();  
            }  
        };  
    }  
};  

The code above works perfectly fine with DataGridBoundColumn. You will have to modify it to use with DataGridTemplateColumn. Merry Christmas and happy coding to all the readers!!!

MESCIUS inc.

comments powered by Disqus