PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/StockTrader RI/Desktop/StockTraderRI/Controls/AnimatedTabControl.Desktop.cs

#
C# | 87 lines | 53 code | 10 blank | 24 comment | 2 complexity | 7520d953659f09f58122c9043b409d24 MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Threading;
  21. namespace StockTraderRI.Controls
  22. {
  23. /// <summary>
  24. /// Custom Tab control with animations.
  25. /// </summary>
  26. /// <remarks>
  27. /// This customization of the TabControl was required to create the animations for the transition
  28. /// between the tab items.
  29. /// </remarks>
  30. public class AnimatedTabControl : TabControl
  31. {
  32. public static readonly RoutedEvent SelectionChangingEvent = EventManager.RegisterRoutedEvent(
  33. "SelectionChanging", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(AnimatedTabControl));
  34. private DispatcherTimer timer;
  35. public AnimatedTabControl()
  36. {
  37. DefaultStyleKey = typeof(AnimatedTabControl);
  38. }
  39. public event RoutedEventHandler SelectionChanging
  40. {
  41. add { AddHandler(SelectionChangingEvent, value); }
  42. remove { RemoveHandler(SelectionChangingEvent, value); }
  43. }
  44. protected override void OnSelectionChanged(SelectionChangedEventArgs e)
  45. {
  46. this.Dispatcher.BeginInvoke(
  47. (Action)delegate
  48. {
  49. this.RaiseSelectionChangingEvent();
  50. this.StopTimer();
  51. this.timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 500) };
  52. EventHandler handler = null;
  53. handler = (sender, args) =>
  54. {
  55. this.StopTimer();
  56. base.OnSelectionChanged(e);
  57. };
  58. this.timer.Tick += handler;
  59. this.timer.Start();
  60. });
  61. }
  62. // This method raises the Tap event
  63. private void RaiseSelectionChangingEvent()
  64. {
  65. var args = new RoutedEventArgs(SelectionChangingEvent);
  66. RaiseEvent(args);
  67. }
  68. private void StopTimer()
  69. {
  70. if (this.timer != null)
  71. {
  72. this.timer.Stop();
  73. this.timer = null;
  74. }
  75. }
  76. }
  77. }