using System; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace Muchinfo.WPF.Controls.Border { public class ExpBorder : System.Windows.Controls.Border { static ExpBorder() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ExpBorder), new FrameworkPropertyMetadata(typeof(ExpBorder))); } //创建动画故事板 // private Storyboard storyboard = new Storyboard(); //Color动画 // private ColorAnimation _colorAnimation = new ColorAnimation(); private System.Timers.Timer _systemTimer = new Timer(); private bool _isInitTime = true; //是否是第一次初始化 public ExpBorder() { BorderThickness = new Thickness(1);//设置默认边框 BorderBrush = Brushes.Transparent; if (IsBorderThickness) Background = Brushes.Transparent; // storyboard.Completed += storyboard_Completed; Margin = new Thickness(1); //_colorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); //_colorAnimation.BeginTime = new TimeSpan(0, 0, 0); //_colorAnimation.RepeatBehavior = new RepeatBehavior(1); //_colorAnimation.AccelerationRatio = 1; _systemTimer.AutoReset = false; _systemTimer.Interval = 1000; _systemTimer.Elapsed += _systemTimer_Elapsed; DataContextChanged += ExpBorder_DataContextChanged; } void _systemTimer_Elapsed(object sender, ElapsedEventArgs e) { //throw new NotImplementedException(); this.Dispatcher.BeginInvoke(new Action(() => { BorderBrush = Brushes.Transparent; if (IsBorderThickness) Background = Brushes.Transparent; })); } //DataGrid虚拟化时没有新增Row而是改变数据源 private void ExpBorder_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (e.OldValue == null) { //第一次初始化时判断是否显示动画 GetChildTextStatus(sender as DependencyObject); } else { // storyboard.Stop(); //停止之前开始的动画 BorderBrush = Brushes.Transparent; if (IsBorderThickness) Background = Brushes.Transparent; _systemTimer.Stop(); _isInitTime = true; //数据源改变时,默认为第一次加载,不进行动画 } } void storyboard_Completed(object sender, EventArgs e) { //storyboard.Stop(); _systemTimer.Stop(); } /// /// 使用故事板创建动画 /// private void CreateAnimationByStoryboard() { var solidcolor = ChangeBrush as SolidColorBrush; //storyboard.Stop(); //storyboard.Children.Clear(); //清除以前动画更新动画 _systemTimer.Stop(); if (solidcolor != null) { BorderBrush = ChangeBrush; if (IsBorderThickness) Background = ChangeBrush; //_colorAnimation.From = solidcolor.Color; //_colorAnimation.To = solidcolor.Color; //Storyboard.SetTarget(_colorAnimation, this); //Storyboard.SetTargetProperty(_colorAnimation, new PropertyPath("(Border.BorderBrush).(SolidColorBrush.Color)")); //storyboard.Children.Add(_colorAnimation); //storyboard.Begin(); _systemTimer.Start(); } } /// /// 用于绑定改变的颜色 /// public Brush ChangeBrush { get { return (Brush)GetValue(ChangeBrushProperty); } set { SetValue(ChangeBrushProperty, value); } } // Using a DependencyProperty as the backing store for ChangeBrush. This enables animation, styling, binding, etc... public static readonly DependencyProperty ChangeBrushProperty = DependencyProperty.Register("ChangeBrush", typeof(Brush), typeof(ExpBorder),new PropertyMetadata(Brushes.Transparent)); //当动画值改变时开始动画 private static void OnVauleChange(DependencyObject d, DependencyPropertyChangedEventArgs e) { var expBorder = d as ExpBorder; // GetChildTextStatus(d); if (expBorder == null) return; if (!expBorder._isInitTime) { expBorder.CreateAnimationByStoryboard(); } else { expBorder._isInitTime = false; } } /// /// 值是否改变 /// public bool IsVauleChange { get { return (bool)GetValue(IsVauleChangeProperty); } set { SetValue(IsVauleChangeProperty, value); } } // Using a DependencyProperty as the backing store for IsVauleChange. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsVauleChangeProperty = DependencyProperty.Register("IsVauleChange", typeof(bool), typeof(ExpBorder), new PropertyMetadata(false, OnVauleChange)); /// /// 是否填充背景(默认只填充边框) /// public bool IsBorderThickness { get { return (bool)GetValue(IsBorderThicknessProperty); } set { SetValue(IsBorderThicknessProperty, value); } } public static readonly DependencyProperty IsBorderThicknessProperty = DependencyProperty.Register("IsBorderThickness", typeof(bool), typeof(ExpBorder), new PropertyMetadata(false)); /// /// 判断是否为第一次加载 /// /// 当前对象 private static void GetChildTextStatus(DependencyObject d) { if (d is ExpBorder) { var border = d as ExpBorder; var textBlock = border.Child as TextBlock; if (textBlock == null) return; border._isInitTime = string.IsNullOrEmpty(textBlock.Text) || "-".Equals(textBlock.Text); } } } }