/// <summary> /// Adds a <see cref="ScaleTransform"/> to the <paramref name="target"/> and sets it's /// origin to the middle. Then it adds two double animations to the <see cref="Storyboard"/> which /// will scale the size of the <paramref name="target"/> from<paramref name="from"/> to <paramref name="to"/>. /// </summary> /// <param name="sb">The <see cref="Storyboard"/> to add the animation to.</param> /// <param name="duration">The duration of the animation.</param> /// <param name="from">The value to animate from.</param> /// <param name="to">The value to animate to.</param> /// <param name="target">The target to run the animation on.</param> public static void AddScaleTransform(this Storyboard sb, TimeSpan duration, double from, double to, UIElement target) { if (target == null) { return; } ScaleTransform scale = new ScaleTransform(1.0, 1.0); target.RenderTransformOrigin = new Point(0.5, 0.5); target.RenderTransform = scale; sb.AddDoubleAnimation(duration, from, to, "RenderTransform.ScaleX", target); sb.AddDoubleAnimation(duration, from, to, "RenderTransform.ScaleY", target); }
public static async Task AnimateAsync(this UIElement view, IAnimation animation) { if (animation is TransformAnimation transformAnimation) { var source = new TaskCompletionSource <bool>(); var curve = transformAnimation.Curve.ToNative(); var from = transformAnimation.From; var to = transformAnimation.To; var w = (float)view.RenderSize.Width; var h = (float)view.RenderSize.Height; var transform = new CompositeTransform() { CenterX = w / 2, CenterY = h / 2, ScaleX = transformAnimation.From.ScaleX, ScaleY = transformAnimation.From.ScaleY, TranslateX = transformAnimation.From.TranslateX, TranslateY = transformAnimation.From.TranslateY, Rotation = transformAnimation.From.Rotation, }; view.Opacity = transformAnimation.From.Opacity; view.RenderTransform = transform; var sb = new Storyboard() { BeginTime = transformAnimation.Delay, Duration = new Duration(transformAnimation.Duration), }; sb.Completed += (s, e) => source.SetResult(true); sb.AddDoubleAnimation(view, "Opacity", transformAnimation.From.Opacity, transformAnimation.To.Opacity, curve); sb.AddDoubleAnimation(transform, "ScaleX", transformAnimation.From.ScaleX, transformAnimation.To.ScaleX, curve); sb.AddDoubleAnimation(transform, "ScaleY", transformAnimation.From.ScaleY, transformAnimation.To.ScaleY, curve); sb.AddDoubleAnimation(transform, "TranslateX", transformAnimation.From.TranslateX * w, transformAnimation.To.TranslateX * w, curve); sb.AddDoubleAnimation(transform, "TranslateY", transformAnimation.From.TranslateY * h, transformAnimation.To.TranslateY * h, curve); sb.AddDoubleAnimation(transform, "Rotation", transformAnimation.From.Rotation, transformAnimation.To.Rotation, curve); sb.Begin(); await source.Task; } else if (animation is SequenceAnimation sequenceAnimation) { foreach (var childAnimation in sequenceAnimation) { await view.AnimateAsync(childAnimation); } } }
public override void EndInit() { base.EndInit(); var button = (ButtonBase)TemplatedParent; Background = button.Background; var disabledContent = new Rectangle { Fill = new SolidColorBrush(((SolidColorBrush)button.Background).Color), RadiusX = 3, RadiusY = 3, Opacity = 0, IsHitTestVisible = false }; var focusContent = new Rectangle { Stroke = new SolidColorBrush(Color.FromArgb(0xFF, 0x6D, 0xBD, 0xD1)), StrokeThickness = 1, RadiusX = 2, RadiusY = 2, Margin = new Thickness(1), Opacity = 0, IsHitTestVisible = false }; var stateGroups = this.GetVisualStateGroups(); var commonStates = new VisualStateGroup(); commonStates.CreateState("Normal"); var mouseOverState = commonStates.CreateState("MouseOver"); var pressedState = commonStates.CreateState("Pressed"); var disabledState = commonStates.CreateState("Disabled"); var checkedState = commonStates.CreateState("Checked"); var focusStates = new VisualStateGroup(); var focusedState = focusStates.CreateState("Focused"); focusStates.CreateState("Unfocused"); stateGroups.Add(commonStates); stateGroups.Add(focusStates); var disabledStoryboard = new Storyboard(); disabledStoryboard.AddDoubleAnimation(disabledContent, x => x.Opacity, .55D); disabledState.Storyboard = disabledStoryboard; var mouseOverStoryboard = new Storyboard(); mouseOverStoryboard.AddColorAnimation(this, x => ((SolidColorBrush)x.Background).Color, Color.FromArgb(0xFF, 0xBF, 0xBF, 0xBF)); mouseOverState.Storyboard = mouseOverStoryboard; var pressedStoryboard = new Storyboard(); pressedStoryboard.AddColorAnimation(this, x => ((SolidColorBrush)x.Background).Color, Color.FromArgb(0xFF, 0x6D, 0xBD, 0xD1)); pressedState.Storyboard = pressedStoryboard; var focusedStoryboard = new Storyboard(); focusedStoryboard.AddDoubleAnimation(focusContent, x => x.Opacity, 1); focusedState.Storyboard = focusedStoryboard; var checkedStoryboard = new Storyboard(); checkedStoryboard.AddColorAnimation(this, x => ((SolidColorBrush)x.Background).Color, Color.FromArgb(0xFF, 0x6D, 0xBD, 0xD1)); checkedState.Storyboard = checkedStoryboard; var contentPresenter = new ContentPresenter { Content = button.Content, ContentTemplate = button.ContentTemplate, VerticalAlignment = button.VerticalContentAlignment, HorizontalAlignment = button.HorizontalContentAlignment, Margin = new Thickness(2, 2, 2, 2) }; SetColumn(contentPresenter, 0); SetRow(contentPresenter, 0); Children.Add(contentPresenter); Children.Add(disabledContent); Children.Add(focusContent); }