本文,没什么内容,主要是记录一下WPF中DataTrigger以及动画的使用。。

 

xaml
 1 <Window x:Class="DataTriggerActionByPropertyChanged.Window1"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     Title="Window1" Height="300" Width="300">
 5     <Window.Resources>
 6         <Style x:Key="ButtonStyle" TargetType="Button">
 7             <Setter Property="Control.Background">
 8                 <Setter.Value>
 9                     <SolidColorBrush Color="SkyBlue"></SolidColorBrush>
10                 </Setter.Value>
11             </Setter>
12             <Style.Triggers> 
13                 <DataTrigger Binding="{Binding Path=Value}" Value="1">
14                     <!--<Setter Property="Background" Value="Red"></Setter>-->
15                     <DataTrigger.EnterActions>
16                         <BeginStoryboard Name="BackgroundChangStoryboard">
17                             <Storyboard>
18                                 <ColorAnimationUsingKeyFrames BeginTime="00:00:00"  RepeatBehavior="Forever"
19                                                                       Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)">
20                                     <DiscreteColorKeyFrame KeyTime="00:00:00.4000000" Value="Red"></DiscreteColorKeyFrame>
21                                     <DiscreteColorKeyFrame KeyTime="00:00:00.8000000" Value="SkyBlue"></DiscreteColorKeyFrame>
22                                 </ColorAnimationUsingKeyFrames>
23                             </Storyboard>
24                         </BeginStoryboard>
25                     </DataTrigger.EnterActions>
26                     <DataTrigger.ExitActions>
27                         <StopStoryboard BeginStoryboardName="BackgroundChangStoryboard"></StopStoryboard>
28                     </DataTrigger.ExitActions>
29                 </DataTrigger>
30             </Style.Triggers>
31         </Style>
32     </Window.Resources>
33     <Grid>
34         <Button Width="100" Height="100" x:Name="button">
35             <Button.Background>
36                 <SolidColorBrush Color="SkyBlue"></SolidColorBrush>
37             </Button.Background>
38             <Button.Style>
39                 <Style >
40                     <!--<Setter Property="Control.Background">
41                         <Setter.Value>
42                             <SolidColorBrush Color="SkyBlue"></SolidColorBrush>
43                         </Setter.Value>
44                     </Setter>-->
45                     <Style.Triggers>
46                         <DataTrigger Binding="{Binding Path=Value}" Value="1">
47                             <!--<Setter Property="Background" Value="Red"></Setter>-->
48                             <DataTrigger.EnterActions>
49                                 <BeginStoryboard Name="BackgroundChangStoryboard">
50                                     <Storyboard>
51                                         <ColorAnimationUsingKeyFrames BeginTime="00:00:00"  RepeatBehavior="Forever"
52                                                                       Storyboard.TargetName="{x:Null}" Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)">
53                                             <DiscreteColorKeyFrame KeyTime="00:00:00.4000000" Value="Red"></DiscreteColorKeyFrame>
54                                             <DiscreteColorKeyFrame KeyTime="00:00:00.8000000" Value="SkyBlue"></DiscreteColorKeyFrame>
55                                         </ColorAnimationUsingKeyFrames>
56                                     </Storyboard>
57                                 </BeginStoryboard>
58                             </DataTrigger.EnterActions>
59                             <DataTrigger.ExitActions>
60                                 <StopStoryboard BeginStoryboardName="BackgroundChangStoryboard"></StopStoryboard>
61                             </DataTrigger.ExitActions>
62                         </DataTrigger>
63                     </Style.Triggers>
64                 </Style>
65             </Button.Style>
66         </Button>
67 
68     </Grid>
69 </Window>
70 

 

后台代码:

 

代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Data;
 8 using System.Windows.Documents;
 9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.ComponentModel;
15 
16 namespace DataTriggerActionByPropertyChanged
17 {
18     /// <summary>
19     /// Window1.xaml 的交互逻辑
20     /// </summary>
21     public partial class Window1 : Window
22     {
23         Test Test = new Test() { Value = 1 };
24         public Window1()
25         {
26             InitializeComponent();
27             this.button.DataContext = Test;
28             System.Timers.Timer timer = new System.Timers.Timer();
29             timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
30             timer.Enabled = true;
31             timer.Interval = 9 * 1000;
32         }
33 
34         void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
35         {
36             Test.Value = DateTime.Now.Second % 2;
37             Dispatcher.Invoke(new Action(delegate() { this.button.DataContext = Test; }), null);
38          
39         } 
40     }
41 
42     public class Test : INotifyPropertyChanged
43     {
44         private int mValue;
45         public int Value
46         {
47             get
48             {
49                 return mValue;
50             }
51             set
52             {
53                 mValue = value;
54                 OnPropertyChanged("Value");
55             }
56         }
57         
58 
59         #region INotifyPropertyChanged 成员
60 
61         public event PropertyChangedEventHandler PropertyChanged;
62         protected void OnPropertyChanged(string propertyName)
63         {
64             if (PropertyChanged != null)
65             {
66                 PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
67             }
68         }
69         #endregion
70     }
71 }
72 

 

 

这个DEMO,需要注意的是:

    Test Test = new Test() { Value = 1 };定义为全局变量以及定义为构造函数以及timer_Elapsed里面的局部变量的不同效果。。

 

下一篇文章,打算写个DEMO 讲解Binding,MultiBinding以及PriorityBinding的不同

 

posted on 2010-01-03 21:53  颜昌钢  阅读(2568)  评论(2编辑  收藏  举报