public void FadeControl(UserControl nextPage, bool fadeIn = true, bool right = true, EventHandler completed = null) { if (nextPage == null) { return; } nextPage.Background = null; nextPage.Name = "A" + string.Join("", Guid.NewGuid().ToByteArray().Select(cur => cur.ToString("X2"))); nextPage.SetBinding(WidthProperty, new Binding("ActualWidth") { Mode = BindingMode.OneWay, Source = canvasMain, }); nextPage.SetBinding(HeightProperty, new Binding("ActualHeight") { Mode = BindingMode.OneWay, Source = canvasMain, }); if (fadeIn) { canvasMain.Children.Add(nextPage); Canvas.SetTop(nextPage, 0); } var duration = new Duration(TimeSpan.FromMilliseconds(100)); var story = new Storyboard(); var fade = new DoubleAnimation { From = fadeIn ? 0.0 : 1.0, To = fadeIn ? 1.0 : 0.0, Duration = duration, }; Storyboard.SetTargetProperty(fade, new PropertyPath(OpacityProperty)); story.Children.Add(fade); var slide = new DoubleAnimation { From = fadeIn ? (right ? 50.0 : -50.0) : 0, To = fadeIn ? 0.0 : (right ? 50.0 : -50.0), Duration = duration, }; Storyboard.SetTargetProperty(slide, new PropertyPath(Canvas.LeftProperty)); story.Children.Add(slide); if (completed != null) { story.Completed += completed; } if (!fadeIn) { story.Completed += (sender, e) => canvasMain.Children.Remove(nextPage); } if (fadeIn && ActiveControl != null) { nextPage.Visibility = Visibility.Collapsed; FadeControl(ActiveControl, false, !right, (sender, e) => { nextPage.Visibility = Visibility.Visible; nextPage.BeginStoryboard(story); }); } else { nextPage.BeginStoryboard(story); } ActiveControl = fadeIn ? nextPage : null; }