WPF 动画显示控件
当我们要显示一个控件的时候,不仅仅要显示这个控件,还要有动画的效果。
主要用到了DoubleAnimation类。
public static void ShowAnimation(object control) { Type type = control.GetType(); switch (type.Name) { case "Border": { Border newBorder = (Border)control; #region 高、宽变化动画 DoubleAnimation widthAnimation = new DoubleAnimation(0, newBorder.Width, new Duration(TimeSpan.FromSeconds(0.5))); newBorder.BeginAnimation(Border.WidthProperty, widthAnimation, HandoffBehavior.Compose); DoubleAnimation heightAnimation = new DoubleAnimation(0, newBorder.Height, new Duration(TimeSpan.FromSeconds(0.5))); newBorder.BeginAnimation(Border.HeightProperty, heightAnimation, HandoffBehavior.Compose); #endregion } break; default: break; } }
调用的时候也简单。
private void button_Click(object sender, RoutedEventArgs e) { bord1.Visibility = Visibility.Visible; ShowAnimation(bord1); }
效果如下:
高山流水,海纳百川!