Monday, 23 August 2010

WPF blinking cell

<Window x:Class="WpfTable.Window6"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window6" Height="158" Width="271">
    <Window.Resources>
        <DataTemplate x:Key="BlinkCellTemplate">                        
            <TextBlock TextAlignment="Center" x:Name="tbMessage" Background="White" Padding="5,0,0,0">
            <TextBlock.Text>
                <Binding Path="Price" NotifyOnTargetUpdated="True">
                    <Binding.StringFormat>{0:#.0000}</Binding.StringFormat>
                </Binding>                
            </TextBlock.Text>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" From="White" To="Red" Duration="0:0:1"></ColorAnimation>
                            <ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" From="Red" To="White" Duration="0:0:1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
            </TextBlock>              
        </DataTemplate>
    </Window.Resources>
    <Grid DataContext="{Binding Path=Model}">
        <StackPanel>           
            <dg:DataGrid Name="_grid" Height="100" AutoGenerateColumns="False" MinColumnWidth="100" SelectionUnit="Cell" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=Tickers}">
                <dg:DataGrid.Columns>
                    <dg:DataGridTextColumn Header="Ticker" Binding="{Binding Path=Name}" Width="100" />
                    <dg:DataGridTemplateColumn Header="Price" CellTemplate="{StaticResource BlinkCellTemplate}" Width="100"/>
                </dg:DataGrid.Columns>
            </dg:DataGrid>
            <Button x:Name="button" Click="button_Click" Content="Press Me"></Button>
        </StackPanel>
    </Grid>
</Window>
public partial class Window6 : Window
    {
        Model6 model = new Model6();
        public Window6()
        {
            InitializeComponent();
            DataContext = this;
            Init();
        }
        public void Init()
        {
            model.Tickers.Add(new Ticker("VOD",100.00));
            model.Tickers.Add(new Ticker("APP", 70.00));
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            foreach (Ticker ticker in model.Tickers)
            {
                ticker.Price = Tick(ticker.Price);
            }
        }
        public Model6 Model
        {
            get { return model; }
        }
        Random random = new Random();
        private double Tick(double value)
        {
            int sign = random.Next(-1, 2);
            double priceChange = 0;
            if(sign!=0)
            {
                priceChange = Math.Round(sign * value * 0.005 * random.Next(0, 10),4);
            }            
            return value + priceChange;
        }
    }
    public class Model6
    {
        private ObservableCollection<Ticker> tickers = new ObservableCollection<Ticker>();
        public ObservableCollection<Ticker> Tickers
        {
            get { return this.tickers; }
        }
    }
    public class Ticker : INotifyPropertyChanged
    {
        private string name;
        private double price;
        public Ticker(string name, double price)
        {
            this.name = name;
            this.price = price;
        }
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged(this, "Name");
            }
        }
        public double Price
        {
            get { return price; }
            set
            {
                price = value;
                OnPropertyChanged(this, "Price");
            }
        }
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(object sender, string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }  

No comments:

Post a Comment