Articles in this section
Category / Section

How to animate GridCell when underlining property gets changed in WPF DataGrid?

1 min read

In WPF DataGrid, it is possible to animate the cell background when changing the underlining property by customizing style for GridCell and writing the behavior for the Grid inside GridCell control template.

The following code example demonstrates how to add behavior in GridCell that animates the background of the cell when underlying property get changed.

XAML

<Style x:Key="GridCellStyle2" TargetType="{x:Type syncfusion:GridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type syncfusion:GridCell}">
                <Grid SnapsToDevicePixels="True">
                    <i:Interaction.Behaviors>
                        <local:AnimateBehavior/>
                    </i:Interaction.Behaviors>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

 

Note: Above code example shows only the partial ControlTemplate of GridCell for reference. You can edit the ControlTemplate of GridCell by right clicking the SfDataGrid in design view and select the Edit Additional Templates -> Edit Cell Style -> Edit a Copy.

You can set this Style to a particular column alone by setting GridColumn.ColumnStyle property of column by defining key for the style. Otherwise it is applied for all the columns.

XAML

<!—Defining SfDataGrid-->
<syncfusion:SfDataGrid x:Name="datagrid"
                                AllowSorting="False" 
                                AutoGenerateColumns="False"
                                ColumnSizer="Star" 
                                ItemsSource="{Binding Stocks}">
<!--Setting GridCell key to SfDataGrid.Columns-->
<syncfusion:SfDataGrid.Columns>     
      <syncfusion:GridTextColumn MappingName="Change" CellStyle="{StaticResource GridCellStyle2}"/>
</syncfusion:SfDataGrid.Columns>

 

You can define the ColourAnimation for animating GridCell. And you can get the GridCell in behavior by using TemplateParent in SfDataGrid. The background of Grid is animated using property changed event.

The following code example shows the AnimatedBehaviour for changing Background of GridCell when underling ViewMode property gets changed.

C#

public class AnimateBehavior : Behavior<Grid>
{
    public string Property { get; set; }
    private GridCell gridCell;
    public ColorAnimation ColorAnimation { get; set; }
    public Brush BackgroundBrush { get; set; }
    protected override void OnAttached()
    {
        BackgroundBrush = new SolidColorBrush { Color = Colors.Transparent };
         //Defining ColourAnimation
        ColorAnimation = new ColorAnimation
            {
                From = Colors.Transparent,
                To = Colors.DarkSlateBlue,
                Duration = new Duration(TimeSpan.FromMilliseconds(400)),
                AutoReverse = true,
                FillBehavior = FillBehavior.Stop
            };
        AssociatedObject.Background = BackgroundBrush;
        gridCell = this.AssociatedObject.TemplatedParent as GridCell;
        gridCell.DataContextChanged += gridCell_DataContextChanged; 
        this.WireEvents();
    }
    void gridCell_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (e.OldValue != null)
        {
            var olddata = e.OldValue as INotifyPropertyChanged;
            if (olddata != null)
                olddata.PropertyChanged -= data_PropertyChanged;
        }
        gridCell = sender as GridCell;
    }
    void WireEvents()
    {
        var data = gridCell.DataContext as INotifyPropertyChanged;
        if (data != null)
            data.PropertyChanged += data_PropertyChanged;
    }
        // PropertyChanged event of SfDataGrid
    void data_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var columnBase = gridCell.ColumnBase;
               if (columnBase.GridColumn.MappingName == e.PropertyName)
        {
            // Animate the Background Color of the GridCell 
            AssociatedObject.Background.BeginAnimation(SolidColorBrush.ColorProperty, ColorAnimation);
        }
    }
    protected override void OnDetaching()
    {
        if (this.AssociatedObject.DataContext == null) return;
        var data = gridCell.DataContext as INotifyPropertyChanged;
        if (data != null)
            data.PropertyChanged -= data_PropertyChanged;
    }
}

 

The following screen shot shows the animated SfDataGrid.

F:\Farjana\F Drive\KB\Animation\WPF.png

You can refer to the following sample link for animating GridCell when underlying property gets changed in SfDataGrid.

Sample Link: AnimateCellGrid_WPF


Conclusion

I hope you enjoyed learning about how to animate the GridCell when underlining property gets changed in WPF DataGrid.

You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied