ExpBorder.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Timers;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. namespace Muchinfo.WPF.Controls.Border
  7. {
  8. public class ExpBorder : System.Windows.Controls.Border
  9. {
  10. static ExpBorder()
  11. {
  12. DefaultStyleKeyProperty.OverrideMetadata(typeof(ExpBorder), new FrameworkPropertyMetadata(typeof(ExpBorder)));
  13. }
  14. //创建动画故事板
  15. // private Storyboard storyboard = new Storyboard();
  16. //Color动画
  17. // private ColorAnimation _colorAnimation = new ColorAnimation();
  18. private System.Timers.Timer _systemTimer = new Timer();
  19. private bool _isInitTime = true; //是否是第一次初始化
  20. public ExpBorder()
  21. {
  22. BorderThickness = new Thickness(1);//设置默认边框
  23. BorderBrush = Brushes.Transparent;
  24. if (IsBorderThickness)
  25. Background = Brushes.Transparent;
  26. // storyboard.Completed += storyboard_Completed;
  27. Margin = new Thickness(1);
  28. //_colorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
  29. //_colorAnimation.BeginTime = new TimeSpan(0, 0, 0);
  30. //_colorAnimation.RepeatBehavior = new RepeatBehavior(1);
  31. //_colorAnimation.AccelerationRatio = 1;
  32. _systemTimer.AutoReset = false;
  33. _systemTimer.Interval = 1000;
  34. _systemTimer.Elapsed += _systemTimer_Elapsed;
  35. DataContextChanged += ExpBorder_DataContextChanged;
  36. }
  37. void _systemTimer_Elapsed(object sender, ElapsedEventArgs e)
  38. {
  39. //throw new NotImplementedException();
  40. this.Dispatcher.BeginInvoke(new Action(() =>
  41. {
  42. BorderBrush = Brushes.Transparent;
  43. if (IsBorderThickness)
  44. Background = Brushes.Transparent;
  45. }));
  46. }
  47. //DataGrid虚拟化时没有新增Row而是改变数据源
  48. private void ExpBorder_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  49. {
  50. if (e.OldValue == null)
  51. {
  52. //第一次初始化时判断是否显示动画
  53. GetChildTextStatus(sender as DependencyObject);
  54. }
  55. else
  56. {
  57. // storyboard.Stop(); //停止之前开始的动画
  58. BorderBrush = Brushes.Transparent;
  59. if (IsBorderThickness)
  60. Background = Brushes.Transparent;
  61. _systemTimer.Stop();
  62. _isInitTime = true; //数据源改变时,默认为第一次加载,不进行动画
  63. }
  64. }
  65. void storyboard_Completed(object sender, EventArgs e)
  66. {
  67. //storyboard.Stop();
  68. _systemTimer.Stop();
  69. }
  70. /// <summary>
  71. /// 使用故事板创建动画
  72. /// </summary>
  73. private void CreateAnimationByStoryboard()
  74. {
  75. var solidcolor = ChangeBrush as SolidColorBrush;
  76. //storyboard.Stop();
  77. //storyboard.Children.Clear(); //清除以前动画更新动画
  78. _systemTimer.Stop();
  79. if (solidcolor != null)
  80. {
  81. BorderBrush = ChangeBrush;
  82. if (IsBorderThickness)
  83. Background = ChangeBrush;
  84. //_colorAnimation.From = solidcolor.Color;
  85. //_colorAnimation.To = solidcolor.Color;
  86. //Storyboard.SetTarget(_colorAnimation, this);
  87. //Storyboard.SetTargetProperty(_colorAnimation, new PropertyPath("(Border.BorderBrush).(SolidColorBrush.Color)"));
  88. //storyboard.Children.Add(_colorAnimation);
  89. //storyboard.Begin();
  90. _systemTimer.Start();
  91. }
  92. }
  93. /// <summary>
  94. /// 用于绑定改变的颜色
  95. /// </summary>
  96. public Brush ChangeBrush
  97. {
  98. get { return (Brush)GetValue(ChangeBrushProperty); }
  99. set { SetValue(ChangeBrushProperty, value); }
  100. }
  101. // Using a DependencyProperty as the backing store for ChangeBrush. This enables animation, styling, binding, etc...
  102. public static readonly DependencyProperty ChangeBrushProperty =
  103. DependencyProperty.Register("ChangeBrush", typeof(Brush), typeof(ExpBorder),new PropertyMetadata(Brushes.Transparent));
  104. //当动画值改变时开始动画
  105. private static void OnVauleChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
  106. {
  107. var expBorder = d as ExpBorder;
  108. // GetChildTextStatus(d);
  109. if (expBorder == null) return;
  110. if (!expBorder._isInitTime)
  111. {
  112. expBorder.CreateAnimationByStoryboard();
  113. }
  114. else
  115. {
  116. expBorder._isInitTime = false;
  117. }
  118. }
  119. /// <summary>
  120. /// 值是否改变
  121. /// </summary>
  122. public bool IsVauleChange
  123. {
  124. get { return (bool)GetValue(IsVauleChangeProperty); }
  125. set { SetValue(IsVauleChangeProperty, value); }
  126. }
  127. // Using a DependencyProperty as the backing store for IsVauleChange. This enables animation, styling, binding, etc...
  128. public static readonly DependencyProperty IsVauleChangeProperty =
  129. DependencyProperty.Register("IsVauleChange", typeof(bool), typeof(ExpBorder), new PropertyMetadata(false, OnVauleChange));
  130. /// <summary>
  131. /// 是否填充背景(默认只填充边框)
  132. /// </summary>
  133. public bool IsBorderThickness
  134. {
  135. get { return (bool)GetValue(IsBorderThicknessProperty); }
  136. set { SetValue(IsBorderThicknessProperty, value); }
  137. }
  138. public static readonly DependencyProperty IsBorderThicknessProperty =
  139. DependencyProperty.Register("IsBorderThickness", typeof(bool), typeof(ExpBorder), new PropertyMetadata(false));
  140. /// <summary>
  141. /// 判断是否为第一次加载
  142. /// </summary>
  143. /// <param name="d">当前对象</param>
  144. private static void GetChildTextStatus(DependencyObject d)
  145. {
  146. if (d is ExpBorder)
  147. {
  148. var border = d as ExpBorder;
  149. var textBlock = border.Child as TextBlock;
  150. if (textBlock == null) return;
  151. border._isInitTime = string.IsNullOrEmpty(textBlock.Text) || "-".Equals(textBlock.Text);
  152. }
  153. }
  154. }
  155. }