tbiro.com

Forums 
   HackPerfCounter
   RADPass
   SHEdit
   Toys
 
   Contact me
   My PGP Key
 

Simple property animation


How to get this code going: Create a Windows Presentation Foundation project named SimpleAnimation and paste the following code in the specified file.


Paste this code into Window1.xaml:

<Window x:Class="SimpleAnimation.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="189" Width="300">
    <Grid>
        <TextBox Height="20" Name="magicNumber" Margin="107,17,52,0" VerticalAlignment="Top"></TextBox>
        <Button Name="validateAge" Height="20" Margin="24,56,54,0" VerticalAlignment="Top" Click="validateAge_Click">Validate</Button>
        <Label Margin="13,16,0,0" Name="label1" Height="26" HorizontalAlignment="Left" VerticalAlignment="Top" Width="91">Magic number
            :</Label>
        <TextBlock Name="label2" Margin="13,0,17,0" TextWrapping="Wrap" Height="60" VerticalAlignment="Bottom">
Enter the magic number (24) to validate or somethign else to see the validation error notification.
</TextBlock>
    </Grid>
</Window>



Paste this code into Window1.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SimpleAnimation
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        private void validateAge_Click(object sender, RoutedEventArgs e)
        {
            if (magicNumber.Text == "24")
            {
                MessageBox.Show("That's right!");
                return;
            }

            // invalid magic number, show the validation visual cues

            // shake

            Thickness from = magicNumber.Margin;
            Thickness to = magicNumber.Margin;

            from.Left -= 5;
            from.Right += 5;

            to.Left += 5;
            to.Right -= 5;

            ThicknessAnimation marginAnimation = new ThicknessAnimation(from, to, TimeSpan.FromMilliseconds(200));
            marginAnimation.RepeatBehavior = new RepeatBehavior(2);
            magicNumber.BeginAnimation(Button.MarginProperty, marginAnimation);


            // flash red

            SolidColorBrush solidColorBrush = new SolidColorBrush();
            solidColorBrush.Color = Colors.White;

            ColorAnimation colorAnimation = new ColorAnimation();
            colorAnimation.From = Colors.White;
            colorAnimation.To = Colors.Red;
            colorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200));
            colorAnimation.AutoReverse = true;
            colorAnimation.RepeatBehavior = new RepeatBehavior(1);

            solidColorBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);

            magicNumber.Background = solidColorBrush;
        }
    }
}

Compile and run.


Comment in the forum





Sitemap generated by Sitemap Manager