WPF TabControl 模拟动画

时间:2021-06-02 23:59:16
  1 using System;
  2 using System.Threading;
  3 using System.Windows;
  4 using System.Windows.Controls;
  5 using WangCai.Common;
  6 
  7 namespace WangCai.Controls
  8 {
  9 
 10     public class MyTabControl : TabControl
 11     {
 12 
 13         Timer t = null;
 14         private int left = 0;
 15         private int AnimationIndex = 1;
 16         protected override void OnSelectionChanged(SelectionChangedEventArgs e)
 17         {
 18             Action a = () =>
 19             {
 20                 try
 21                 {
 22                     AnimationIndex++;
 23                     var control = e.Source as MyTabControl;
 24                     if (control == null) return;
 25                     base.OnSelectionChanged(e);
 26                     if (AnimationIndex % 2 == 0)
 27                     {
 28                         left = -100;
 29                     }
 30                     else
 31                     {
 32                         left = 100;
 33                     }
 34 
 35                     var selectItem = control.SelectedContent as ScrollViewer;
 36                     if (selectItem == null)
 37                     {
 38 
 39                         var selectItem_2 = control.SelectedContent as Grid;
 40                         if (selectItem_2 == null)
 41                         {
 42                             var selectItem_3 = control.SelectedContent as StackPanel;
 43                             if (selectItem_3 == null) return;
 44                             selectItem_3.Visibility = Visibility.Collapsed;
 45                             t = new Timer(Move, selectItem_3, 0, 5);
 46                             return;
 47                         }
 48                         selectItem_2.Visibility = Visibility.Collapsed;
 49                         t = new Timer(Move, selectItem_2, 0, 5);
 50                         return;
 51                     }
 52                     selectItem.Visibility = Visibility.Collapsed;
 53 
 54                     t = new Timer(Move, selectItem, 0, 2);
 55 
 56                 }
 57                 catch (Exception)
 58                 {
 59 
 60                 }
 61             };
 62             this.Dispatcher.BeginInvoke(a);
 63         }
 64 
 65 
 66         public void Move(object o)
 67         {
 68 
 69             Action a = () =>
 70             {
 71                 try
 72                 {
 73                     if (AnimationIndex % 2 == 0)
 74                     {
 75                         left +=6;
 76                         if (left > 5)
 77                         {
 78                             t.Dispose();
 79 
 80                             return;
 81                         }
 82                         AnimationIndex = 0;
 83                     }
 84                     else
 85                     {
 86 
 87                         left -= 6;
 88 
 89                         if (left <= 0)
 90                         {
 91                             t.Dispose();
 92 
 93                             return;
 94                         }
 95                         AnimationIndex = 1;
 96                     }
 97 
 98                     var selectItem = o as ScrollViewer;
 99                     if (selectItem == null)
100                     {
101                         var selectItem_2 = o as Grid;
102                         if (selectItem_2 == null)
103                         {
104                             var selectItem_3 = o as StackPanel;
105                             if (selectItem_3 == null) return;
106                             selectItem_3.Visibility = Visibility.Visible;
107                             selectItem_3.Margin = new Thickness(left, 0, 0, 0);
108 
109                             return;
110                         }
111                         selectItem_2.Visibility = Visibility.Visible;
112                         selectItem_2.Margin = new Thickness(left, 0, 0, 0);
113                         return;
114                     }
115                     selectItem.Visibility = Visibility.Visible;
116                     selectItem.Margin = new Thickness(left, 0, 0, 0);
117                 }
118                 catch (Exception)
119                 {
120 
121                 }
122 
123             };
124             this.Dispatcher.BeginInvoke(a);
125         }
126 
127 
128     }
129 }