From fa318ab51d9ec90ffe37a3ee444cbad2470bee9c Mon Sep 17 00:00:00 2001 From: Jan Karger Date: Tue, 30 Jun 2015 23:27:10 +0200 Subject: [PATCH 1/2] use MetroProgressBar in DataGrid, update progress sample --- .../ExampleViews/DataGridExamples.xaml | 25 ++++++++++++++----- .../ExampleViews/SliderProgressExamples.xaml | 19 +++++++++----- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/samples/MetroDemo/ExampleViews/DataGridExamples.xaml b/samples/MetroDemo/ExampleViews/DataGridExamples.xaml index e9d2aa0b70..47e6ffe0bb 100644 --- a/samples/MetroDemo/ExampleViews/DataGridExamples.xaml +++ b/samples/MetroDemo/ExampleViews/DataGridExamples.xaml @@ -194,8 +194,10 @@ - - + + @@ -226,10 +228,21 @@ Binding="{Binding Artist.Name}" /> - + + + + + + + diff --git a/samples/MetroDemo/ExampleViews/SliderProgressExamples.xaml b/samples/MetroDemo/ExampleViews/SliderProgressExamples.xaml index a7465ab86f..f347067455 100644 --- a/samples/MetroDemo/ExampleViews/SliderProgressExamples.xaml +++ b/samples/MetroDemo/ExampleViews/SliderProgressExamples.xaml @@ -108,7 +108,9 @@ SmallChange="0.1" LowerValue="-70" UpperValue="60" /> - + - - - + Date: Tue, 30 Jun 2015 23:32:36 +0200 Subject: [PATCH 2/2] fix the Animation Warnings System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Stop'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='39750236'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Controls.Grid'; TargetElement.HashCode='19146950'; TargetElement.Type='System.Windows.Controls.Grid' System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Remove'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='39750236'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='System.Windows.Controls.Grid'; TargetElement.HashCode='19146950'; TargetElement.Type='System.Windows.Controls.Grid' --- MahApps.Metro/Controls/MetroProgressBar.cs | 114 +++++++++++++-------- 1 file changed, 70 insertions(+), 44 deletions(-) diff --git a/MahApps.Metro/Controls/MetroProgressBar.cs b/MahApps.Metro/Controls/MetroProgressBar.cs index 0c5a70de75..8e2af4c519 100644 --- a/MahApps.Metro/Controls/MetroProgressBar.cs +++ b/MahApps.Metro/Controls/MetroProgressBar.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Linq; using System.Windows; using System.Windows.Controls; @@ -21,6 +20,7 @@ public class MetroProgressBar : ProgressBar DependencyProperty.Register("EllipseOffset", typeof(double), typeof(MetroProgressBar), new PropertyMetadata(default(double))); + private readonly object lockme = new object(); private Storyboard indeterminateStoryboard; static MetroProgressBar() @@ -29,35 +29,38 @@ static MetroProgressBar() IsIndeterminateProperty.OverrideMetadata(typeof(MetroProgressBar), new FrameworkPropertyMetadata(OnIsIndeterminateChanged)); } - public MetroProgressBar() - { - SizeChanged += SizeChangedHandler; - IsVisibleChanged += VisibleChangedHandler; - } - - void VisibleChangedHandler(object sender, DependencyPropertyChangedEventArgs e) + private void VisibleChangedHandler(object sender, DependencyPropertyChangedEventArgs e) { //reset Storyboard if Visibility is set to Visible #1300 - if (e.NewValue is bool && (bool)e.NewValue) + if (IsIndeterminate) { - ResetStoryboard(ActualWidth); + ToggleIndeterminate(this, (bool)e.OldValue, (bool)e.NewValue); } } private static void OnIsIndeterminateChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { - var bar = dependencyObject as MetroProgressBar; - if (bar != null && e.NewValue != e.OldValue) + ToggleIndeterminate(dependencyObject as MetroProgressBar, (bool)e.OldValue, (bool)e.NewValue); + } + + private static void ToggleIndeterminate(MetroProgressBar bar, bool oldValue, bool newValue) + { + if (bar != null && newValue != oldValue) { var indeterminateState = bar.GetIndeterminate(); var containingObject = bar.GetTemplateChild("ContainingGrid") as FrameworkElement; if (indeterminateState != null && containingObject != null) { - if (indeterminateState.Storyboard != null) + if (oldValue && indeterminateState.Storyboard != null) { + // remove the previous storyboard from the Grid #1855 indeterminateState.Storyboard.Stop(containingObject); + indeterminateState.Storyboard.Remove(containingObject); + } + if (newValue) + { + bar.ResetStoryboard(bar.ActualWidth, false); } - bar.ResetStoryboard(bar.ActualWidth); } } } @@ -82,37 +85,41 @@ public double EllipseOffset private void SizeChangedHandler(object sender, SizeChangedEventArgs e) { - double actualWidth = ActualWidth; - MetroProgressBar bar = this; - if (this.Visibility == Visibility.Visible) + var actualWidth = ActualWidth; + var bar = this; + if (this.Visibility == Visibility.Visible && this.IsIndeterminate) { - bar.ResetStoryboard(actualWidth); + bar.ResetStoryboard(actualWidth, true); } } - private void ResetStoryboard(double width) + private void ResetStoryboard(double width, bool removeOldStoryboard) { - lock (this) + if (!this.IsIndeterminate) + { + return; + } + lock (this.lockme) { //perform calculations - double containerAnimStart = CalcContainerAnimStart(width); - double containerAnimEnd = CalcContainerAnimEnd(width); - double ellipseAnimWell = CalcEllipseAnimWell(width); - double ellipseAnimEnd = CalcEllipseAnimEnd(width); + var containerAnimStart = CalcContainerAnimStart(width); + var containerAnimEnd = CalcContainerAnimEnd(width); + var ellipseAnimWell = CalcEllipseAnimWell(width); + var ellipseAnimEnd = CalcEllipseAnimEnd(width); //reset the main double animation try { - VisualState indeterminate = GetIndeterminate(); + var indeterminate = GetIndeterminate(); if (indeterminate != null && this.indeterminateStoryboard != null) { - Storyboard newStoryboard = this.indeterminateStoryboard.Clone(); - Timeline doubleAnim = newStoryboard.Children.First(t => t.Name == "MainDoubleAnim"); + var newStoryboard = this.indeterminateStoryboard.Clone(); + var doubleAnim = newStoryboard.Children.First(t => t.Name == "MainDoubleAnim"); doubleAnim.SetValue(DoubleAnimation.FromProperty, containerAnimStart); doubleAnim.SetValue(DoubleAnimation.ToProperty, containerAnimEnd); var namesOfElements = new[] { "E1", "E2", "E3", "E4", "E5" }; - foreach (string elemName in namesOfElements) + foreach (var elemName in namesOfElements) { var doubleAnimParent = (DoubleAnimationUsingKeyFrames)newStoryboard.Children.First(t => t.Name == elemName + "Anim"); DoubleKeyFrame first, second, third; @@ -142,7 +149,7 @@ private void ResetStoryboard(double width) var containingGrid = (FrameworkElement)GetTemplateChild("ContainingGrid"); - if (indeterminate.Storyboard != null) + if (removeOldStoryboard && indeterminate.Storyboard != null) { // remove the previous storyboard from the Grid #1855 indeterminate.Storyboard.Stop(containingGrid); @@ -151,11 +158,6 @@ private void ResetStoryboard(double width) indeterminate.Storyboard = newStoryboard; - if (!IsIndeterminate) - { - return; - } - if (indeterminate.Storyboard != null) { indeterminate.Storyboard.Begin(containingGrid, true); @@ -176,15 +178,14 @@ private VisualState GetIndeterminate() { return null; } - IList groups = VisualStateManager.GetVisualStateGroups(templateGrid); + var groups = VisualStateManager.GetVisualStateGroups(templateGrid); return groups != null - ? groups.Cast() - .SelectMany(@group => @group.States.Cast()) - .FirstOrDefault(state => state.Name == "Indeterminate") - : null; + ? groups.Cast() + .SelectMany(@group => @group.States.Cast()) + .FirstOrDefault(state => state.Name == "Indeterminate") + : null; } - private void SetEllipseDiameter(double width) { if (width <= 180) @@ -220,20 +221,28 @@ private void SetEllipseOffset(double width) private double CalcContainerAnimStart(double width) { if (width <= 180) + { return -34; + } if (width <= 280) + { return -50.5; + } return -63; } private double CalcContainerAnimEnd(double width) { - double firstPart = 0.4352 * width; + var firstPart = 0.4352 * width; if (width <= 180) + { return firstPart - 25.731; + } if (width <= 280) + { return firstPart + 27.84; + } return firstPart + 58.862; } @@ -248,12 +257,25 @@ private double CalcEllipseAnimEnd(double width) return width * 2.0 / 3.0; } - public override void OnApplyTemplate() { base.OnApplyTemplate(); - indeterminateStoryboard = this.TryFindResource("IndeterminateStoryboard") as Storyboard; + + lock (this.lockme) + { + this.indeterminateStoryboard = this.TryFindResource("IndeterminateStoryboard") as Storyboard; + } + + Loaded -= LoadedHandler; + Loaded += LoadedHandler; + } + + private void LoadedHandler(object sender, RoutedEventArgs routedEventArgs) + { + Loaded -= LoadedHandler; SizeChangedHandler(null, null); + SizeChanged += SizeChangedHandler; + IsVisibleChanged += VisibleChangedHandler; } protected override void OnInitialized(EventArgs e) @@ -263,9 +285,13 @@ protected override void OnInitialized(EventArgs e) // Update the Ellipse properties to their default values // only if they haven't been user-set. if (EllipseDiameter.Equals(0)) + { SetEllipseDiameter(ActualWidth); + } if (EllipseOffset.Equals(0)) + { SetEllipseOffset(ActualWidth); + } } } -} +} \ No newline at end of file