Then later in my window I have a grid that contains the DataGrid:
<DataGrid AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding MyList}">
<DataGrid.Columns>
<DataGridTextColumn Header="Strike" Binding="{Binding ExpirationPrice}"/>
<DataGridTemplateColumn Header="My Property" CellTemplate="{StaticResource FlashingCellMyProperty}"/>
</DataGrid.Columns>
</DataGrid>
This works fine but it seem to me a little wasteful to have a data template for each property. Optimally I'd like to have a way to define a "style" or a control template or somehow change existing "stock" column (e.g. DataGridTextColumn) and only change the binding on each column.
I've search a lot online but didn't find the right path to follow. I am new to WPF and trying to read but there's much to read and I am not sure what subject I need to learn.
Thanks!
Last edited by tomercagan; August 22nd, 2011 at 06:28 AM.
Reason: Mistake in ItemSource used
Re: Flashing DataGrid Cell - How to do it properly
OK.
After spending a few more hours on this matter I've found a way of doing it using a style.
In resources you define a style. The style must target TextBlock (I guess underlaying control in a DataGridTextColumn:
<Style TargetType="{x:Type TextBlock}" x:Key="MyStyle">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Transparent"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard HandoffBehavior="Compose">
<Storyboard TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
<ColorAnimation Duration="0:0:0.03" To="Red" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
Then in your datagrid you just set the style of the columm:
<DataGridTextColumn Header="Value" Binding="{Binding Value, Mode=OneWay, NotifyOnTargetUpdated=True}" ElementStyle="{StaticResource MyStyle}"/>
A few things to note:
* In the style you must set the background with a solid brush instance. Transperant color may be a good choice.
* In the story board you update the target property using the full qualification. Note the end is SolidColorBrush.Color. Have we not set it before an exception would have been thrown - that's becuase the initial value of the background wasn't a solid brush and the cast required in the target property would have faild.
* For a "complete" animation it is advisable to set BeginStoryboard's HandoffBehavior to "Compose". This prevents the background color to eventually stay red in case of many consecutive events being triggered. Another approach I've seen was using StopStoryBoard before BeginStoryBoard but didn't try.
* In the DataGridTextColumn Binding you must set NotifyOnTargetUpdated=True or the event doesn't fire...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.