PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
C# | 78 lines | 52 code | 7 blank | 19 comment | 11 complexity | e997371692003e460a04d6a5d6e580a4 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.Collections.ObjectModel;
18using System.Collections.Specialized;
19using System.Windows;
20using System.Windows.Controls;
21using System;
22
23namespace StockTraderRI.Controls
24{
25    /// <summary>
26    /// Custom ItemsControl with Header.
27    /// </summary>
28    public class ResearchControl : ItemsControl
29    {
30        public static readonly DependencyProperty HeadersProperty =
31            DependencyProperty.Register("Headers", typeof(ObservableCollection<object>), typeof(ResearchControl), null);
32
33        public ResearchControl()
34        {
35            this.Headers = new ObservableCollection<object>();
36        }
37
38        public ObservableCollection<object> Headers
39        {
40            get { return (ObservableCollection<object>)GetValue(HeadersProperty); }
41            private set { SetValue(HeadersProperty, value); }
42        }
43
44        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
45        {
46            if (e == null)
47            {
48                throw new ArgumentNullException("e");
49            }
50
51            base.OnItemsChanged(e);
52            if (e.Action == NotifyCollectionChangedAction.Add)
53            {
54                object newItem = e.NewItems[0];
55                DependencyObject header = GetHeader(newItem as FrameworkElement);
56                this.Headers.Insert(e.NewStartingIndex, header);
57            }
58            else if (e.Action == NotifyCollectionChangedAction.Remove)
59            {
60                this.Headers.RemoveAt(e.OldStartingIndex);
61            }
62        }
63
64        private static DependencyObject GetHeader(FrameworkElement view)
65        {
66            if (view != null)
67            {
68                DataTemplate template = view.Resources["HeaderIcon"] as DataTemplate;
69                if (template != null)
70                {
71                    return template.LoadContent();
72                }
73            }
74
75            return null;
76        }
77    }
78}