/// <summary> /// 生成一个 SplineThicknessKeyFrame 对象 /// </summary> /// <param name="kTime">应到达关键帧的目标 System.Windows.Media.Animation.ThicknessKeyFrame.Value 的时间</param> /// <param name="ellipseLeftMarginVal">关键帧的目标值</param> /// <returns>返回一个新的 SplineThicknessKeyFrame 对象</returns> private SplineThicknessKeyFrame CreateSplineThicknessKeyFrame(KeyTime kTime, Thickness ellipseLeftMarginVal) { SplineThicknessKeyFrame sThicknessKeyFrame = new SplineThicknessKeyFrame(); sThicknessKeyFrame.KeyTime = kTime; sThicknessKeyFrame.Value = ellipseLeftMarginVal; return(sThicknessKeyFrame); }
/// <summary> /// Start thinkness animation. /// </summary> /// <param name="parent">Object that contains property.</param> /// <param name="propertyName">A name of the property.</param> /// <param name="propertyPath">A path that describe the dependency property to be animated.</param> /// <param name="duration">How many time would take transit.</param> /// <param name="from">Start value.</param> /// <param name="to">Finish value.</param> /// <param name="fillBehavior"> /// Specifies how a System.Windows.Media.Animation.Timeline behaves when it is outside /// its active period but its parent is inside its active or hold period.</param> /// <param name="initHandler">Handler that would be called before animation start. /// There you can subscrube on events or reconfigurate settigns.</param> /// <returns>Created storyboard.</returns> public static Storyboard StartStoryboard( FrameworkElement parent, string propertyName, PropertyPath propertyPath, TimeSpan duration, Thickness from, Thickness to, FillBehavior fillBehavior, Action <Storyboard> initHandler) { // Create a storyboard to contains the animations. Storyboard storyboard = new Storyboard { FillBehavior = fillBehavior }; // Add the animation to the storyboard ThicknessAnimationUsingKeyFrames animation = new ThicknessAnimationUsingKeyFrames(); storyboard.Children.Add(animation); animation.Duration = new Duration(duration); animation.AccelerationRatio = 1.0f; // Set start position. SplineThicknessKeyFrame startKey = new SplineThicknessKeyFrame( from, KeyTime.FromPercent(0)); //// Set start position. //SplineThicknessKeyFrame middleKey = new SplineThicknessKeyFrame( // new Thickness(Lerp(from.Left, to.Right, ), // KeyTime.FromPercent(0.62)); // Set finish position. SplineThicknessKeyFrame finishKey = new SplineThicknessKeyFrame( to, KeyTime.FromPercent(1)); // Configure the animation to target de property Opacity Storyboard.SetTargetName(animation, propertyName); Storyboard.SetTargetProperty(animation, propertyPath); // Add keys. animation.KeyFrames.Add(startKey); //animation.KeyFrames.Add(middleKey); animation.KeyFrames.Add(finishKey); // Inform subscribers. initHandler?.Invoke(storyboard); // Begin the storyboard try { storyboard.Begin(parent); } catch (Exception ex) { MessageBox.Show(ex.Message); } return(storyboard); }
private void TextBox_GotFocus(object sender, RoutedEventArgs e) { double fleft, ftop, fwidth; TextBox ntb = (TextBox)sender; if (tb == null) { fleft = ntb.Margin.Left; ftop = ntb.Margin.Top; fwidth = ntb.Width; } else { //获得失去焦点的文本框的位置 fleft = tb.Margin.Left; ftop = tb.Margin.Top; fwidth = tb.Width; } //获得获得焦点的文本框的位置 double nleft = ntb.Margin.Left; double ntop = ntb.Margin.Top; double nwidth = ntb.Width; //开启动画效果 //ThicknessAnimation ta = new ThicknessAnimation(); SplineThicknessKeyFrame stk1 = new SplineThicknessKeyFrame(); SplineThicknessKeyFrame stk2 = new SplineThicknessKeyFrame(); ThicknessAnimationUsingKeyFrames du = new ThicknessAnimationUsingKeyFrames(); stk1.Value = new Thickness(fleft, ftop, 0, 0); stk2.Value = new Thickness(nleft, ntop, 0, 0); stk1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)); stk2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(400)); stk2.KeySpline = new KeySpline(0, 0, 0, 1); du.KeyFrames.Add(stk1); du.KeyFrames.Add(stk2); //ta.From = new Thickness(fleft,ftop,0,0); //ta.To = new Thickness(nleft, ntop, 0, 0); //ta.Duration = new Duration(TimeSpan.FromMilliseconds(200)); Storyboard.SetTargetName(du, "Margin"); Storyboard.SetTargetProperty(du, new PropertyPath(Border.MarginProperty)); Storyboard storyBoard = new Storyboard(); storyBoard.Children.Add(du); this.RegisterName("Margin", this.ColorFulBorder); DoubleAnimation ta = new DoubleAnimation(); ta.From = 0; ta.To = 0.5; ta.Duration = new Duration(TimeSpan.FromMilliseconds(100)); Storyboard.SetTargetName(ta, "Opacity"); Storyboard.SetTargetProperty(ta, new PropertyPath(Border.OpacityProperty)); Storyboard storyBoard2 = new Storyboard(); storyBoard2.Children.Add(ta); this.RegisterName("Opacity", this.ColorFulBorder); storyBoard2.Begin(this, true); DoubleAnimation tw = new DoubleAnimation(); tw.From = fwidth; tw.To = nwidth; tw.Duration = new Duration(TimeSpan.FromMilliseconds(100)); Storyboard.SetTargetName(tw, "Width"); Storyboard.SetTargetProperty(tw, new PropertyPath(Border.WidthProperty)); Storyboard storyBoard3 = new Storyboard(); storyBoard3.Children.Add(tw); this.RegisterName("Width", this.ColorFulBorder); storyBoard3.Begin(this, true); storyBoard.Begin(this, true); }