private void MainCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { finalMouseX = e.GetPosition(MainCanvas).X; double diff = Math.Abs(finalMouseX - initMouseX); // Make sure the diff is substantial so that tiles // don't scroll on double-click. if ((diff > 5)) { if ((finalMouseX < initMouseX)) { newX = x - (diff * 2); } else if ((finalMouseX > initMouseX)) { newX = x + (diff * 2); } anim.KeyFrames.Add(new SplineDoubleKeyFrame(newX, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)), new KeySpline(0.161, 0.079, 0.008, 1))); anim.FillBehavior = FillBehavior.HoldEnd; MetroStackPanel.BeginAnimation(Canvas.LeftProperty, anim); anim.KeyFrames.Clear(); timer.Start(); } }
// Check whether the StackPanel is no longer in view and // return it to a suitable postion. private void timer_Tick(object sender, EventArgs e) { double mspWidth = MetroStackPanel.ActualWidth; if ((newX > 200)) { anim.KeyFrames.Add(new SplineDoubleKeyFrame(45, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)), new KeySpline(0.161, 0.079, 0.008, 1))); anim.FillBehavior = FillBehavior.HoldEnd; MetroStackPanel.BeginAnimation(Canvas.LeftProperty, anim); anim.KeyFrames.Clear(); } else if (((newX + mspWidth) < 500)) { double widthX = 500 - (newX + mspWidth); double shiftX = newX + widthX; anim.KeyFrames.Add(new SplineDoubleKeyFrame(shiftX, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(1)), new KeySpline(0.161, 0.079, 0.008, 1))); anim.FillBehavior = FillBehavior.HoldEnd; MetroStackPanel.BeginAnimation(Canvas.LeftProperty, anim); anim.KeyFrames.Clear(); } timer.Stop(); }