I have a pretty simple wpftoolkit:datagrid to show stock market bid and ask.
My grid is bound to an ObservableCollection<PriceViewModel>
. My PriceViewModel
implements INotifyPropertyChanged
.
The grid correctly updates and I have managed to get the background color to animate but it is intermittent in apply the animation.
Below is the XAML and snippet of the view model class.
The idea is just to color red when a price update is lower than the previous and green when it's higher...nothing too fancy.
<WpfToolkit:DataGrid Name="PriceDataGrid" RowHeaderWidth="5"
AutoGenerateColumns="False" VerticalContentAlignment="Center" Margin="0,33,0,0" HorizontalAlignment="Left" Width="868">
<WpfToolkit:DataGrid.Columns>
<WpfToolkit:DataGridTemplateColumn Header="Bid" MinWidth="40">
<WpfToolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Bid}" Margin="3,1" x:Name="txtTextBlock">
<TextBlock.Background>
<SolidColorBrush Color="Transparent"></SolidColorBrush>
</TextBlock.Background>
</TextBlock>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding BidUp}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
BeginTime="00:00:00"
Duration="0:0:0.1"
To="Green"
AutoReverse="True"
Storyboard.TargetName="txtTextBlock"
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding BidDown}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
BeginTime="00:00:00"
Duration="0:0:0.1"
To="Red"
AutoReverse="True"
Storyboard.TargetName="txtTextBlock"
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</WpfToolkit:DataGridTemplateColumn.CellTemplate>
</WpfToolkit:DataGridTemplateColumn>
<WpfToolkit:DataGridTextColumn Header="Ask" Binding="{Binding Path=Ask}" MinWidth="40" />
</WpfToolkit:DataGrid.Columns>
</WpfToolkit:DataGrid>
And the view model:
public class PriceViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
Price _price;
private bool _bidUp = false;
private bool _bidDown = false;
public bool BidUp
{
get
{
return _bidUp;
}
set
{
_bidUp = value;
OnPropertyChanged("BidUp");
}
}
public bool BidDown
{
get
{
return _bidDown;
}
set
{
_bidDown = value;
OnPropertyChanged("BidDown");
}
}
public double Bid
{
get { return _price.Bid; }
set
{
BidUp = (value > _price.Bid);
BidDown = (value < _price.Bid);
_price.Bid = value;
OnPropertyChanged("Bid");
}
}
public double Ask
{
get { return _price.Ask; }
set
{
AskUp = (value > _price.Ask);
_price.Ask = value;
OnPropertyChanged("Ask");
}
}
public PriceViewModel(Price price)
{
_price = price;
}
private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}