PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms 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//===================================================================================
17using System;
18using System.Windows;
19using System.Windows.Controls;
20using System.Windows.Threading;
21
22namespace StockTraderRI.Controls
23{
24    /// <summary>
25    /// Custom Tab control with animations.
26    /// </summary>
27    /// <remarks>
28    /// This customization of the TabControl was required to create the animations for the transition 
29    /// between the tab items.
30    /// </remarks>
31    public class AnimatedTabControl : TabControl
32    {
33        public static readonly RoutedEvent SelectionChangingEvent = EventManager.RegisterRoutedEvent(
34            "SelectionChanging", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(AnimatedTabControl));
35
36        private DispatcherTimer timer;
37
38        public AnimatedTabControl()
39        {
40            DefaultStyleKey = typeof(AnimatedTabControl);
41        }
42
43        public event RoutedEventHandler SelectionChanging
44        {
45            add { AddHandler(SelectionChangingEvent, value); }
46            remove { RemoveHandler(SelectionChangingEvent, value); }
47        }
48
49        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
50        {
51            this.Dispatcher.BeginInvoke(
52                (Action)delegate
53                {
54                    this.RaiseSelectionChangingEvent();
55
56                    this.StopTimer();
57
58                    this.timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 500) };
59
60                    EventHandler handler = null;
61                    handler = (sender, args) =>
62                        {
63                            this.StopTimer();
64                            base.OnSelectionChanged(e);
65                        };
66                    this.timer.Tick += handler;
67                    this.timer.Start();
68                });
69        }
70
71        // This method raises the Tap event
72        private void RaiseSelectionChangingEvent()
73        {
74            var args = new RoutedEventArgs(SelectionChangingEvent);
75            RaiseEvent(args);
76        }
77
78        private void StopTimer()
79        {
80            if (this.timer != null)
81            {
82                this.timer.Stop();
83                this.timer = null;
84            }
85        }
86    }
87}