PageRenderTime 39ms CodeModel.GetById 19ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/V1/trunk/Source/ChartControls/LabelExtractor.cs

#
C# | 163 lines | 119 code | 24 blank | 20 comment | 23 complexity | 64648d4d7d6ab9e5202edc6b92b7b81e MD5 | raw file
  1//===============================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation
  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
 18using System.Windows;
 19using System.Collections.ObjectModel;
 20using System.Windows.Data;
 21using System;
 22using System.Windows.Controls;
 23using System.Collections.Specialized;
 24
 25namespace StockTraderRI.ChartControls
 26{
 27    public class LabelExtractor : Freezable
 28    {
 29        protected override Freezable CreateInstanceCore()
 30        {
 31            return new LabelExtractor();
 32        }
 33
 34        private static void OnItemsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
 35        {
 36            LabelExtractor v = sender as LabelExtractor;
 37            ItemCollection oldItems = args.OldValue as ItemCollection;
 38            ItemCollection newItems = args.NewValue as ItemCollection;
 39            if (oldItems != null)
 40                ((INotifyCollectionChanged)oldItems).CollectionChanged -= new NotifyCollectionChangedEventHandler(v.OnLabelsCollectionChanged);
 41
 42            if (v != null && v.Items != null)
 43            {
 44                ((INotifyCollectionChanged)v.Items).CollectionChanged += new NotifyCollectionChangedEventHandler(v.OnLabelsCollectionChanged);
 45                if (v.LabelPath != null)
 46                    v.GenerateLabelList();
 47            }
 48        }
 49
 50        private static void OnLabelPathChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
 51        {
 52            LabelExtractor v = sender as LabelExtractor;
 53            if (v != null && v.LabelPath != null && v.Items != null)
 54            {
 55                v.GenerateLabelList();
 56            }
 57        }
 58        
 59        private void OnLabelsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
 60        {
 61            if (e.Action.Equals(NotifyCollectionChangedAction.Reset))
 62            {
 63                GenerateLabelList();
 64            }
 65            else if (e.Action.Equals(NotifyCollectionChangedAction.Remove))
 66            {
 67                for (int i = 0; i < e.OldItems.Count; i++)
 68                {
 69                    Labels.RemoveAt(e.OldStartingIndex);
 70                }
 71            }
 72            else if (e.Action.Equals(NotifyCollectionChangedAction.Move))
 73            {
 74                Labels.Move(e.OldStartingIndex, e.NewStartingIndex);
 75            }
 76            else
 77            {
 78                for (int i = 0; i < e.NewItems.Count; i++)
 79                {
 80                    CreateInternalBinding(Items[e.NewStartingIndex + i]);
 81                    if (e.Action.Equals(NotifyCollectionChangedAction.Add))
 82                        Labels.Insert(e.NewStartingIndex + i, LabelHolder);
 83                    else
 84                        Labels[e.NewStartingIndex + i] = LabelHolder;
 85                }
 86            }
 87        }
 88        
 89        private void GenerateLabelList()
 90        {
 91            SetValue(LabelsKey, new ObservableCollection<string>());
 92
 93            ObservableCollection<String> tempLabels = Labels;
 94            foreach (Object o in Items)
 95            {
 96                CreateInternalBinding(o);
 97                tempLabels.Add((String)LabelHolder);
 98            }
 99        }
100
101        private void CreateInternalBinding(Object source)
102        {
103            Binding b = new Binding();
104            b.Source = source;
105            if (IsXmlNodeHelper(source))
106                b.XPath = LabelPath.Path;
107            else
108                b.Path = LabelPath;
109            BindingOperations.SetBinding(this, LabelExtractor.LabelHolderProperty, b);
110        }
111
112        private static bool IsXmlNodeHelper(object item)
113        {
114            return item is System.Xml.XmlNode;
115        }
116
117        public ItemCollection Items
118        {
119            get { return (ItemCollection)GetValue(ItemsProperty); }
120            set { SetValue(ItemsProperty, value); }
121        }
122
123        // Using a DependencyProperty as the backing store for Items.  This enables animation, styling, binding, etc...
124        public static readonly DependencyProperty ItemsProperty =
125            DependencyProperty.Register("Items", typeof(ItemCollection), typeof(LabelExtractor), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnItemsChanged)));
126
127
128        public PropertyPath LabelPath
129        {
130            get { return (PropertyPath)GetValue(LabelPathProperty); }
131            set { SetValue(LabelPathProperty, value); }
132        }
133
134        // Using a DependencyProperty as the backing store for LabelPath.  This enables animation, styling, binding, etc...
135        public static readonly DependencyProperty LabelPathProperty =
136            DependencyProperty.Register("LabelPath", typeof(PropertyPath), typeof(LabelExtractor), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnLabelPathChanged)));
137
138
139        public ObservableCollection<String> Labels
140        {
141            get { return (ObservableCollection<String>)GetValue(LabelsProperty); }
142        }
143
144        // Using a DependencyProperty as the backing store for Labels.  This enables animation, styling, binding, etc...
145        private static readonly DependencyPropertyKey LabelsKey =
146            DependencyProperty.RegisterReadOnly("Labels", typeof(ObservableCollection<String>), typeof(LabelExtractor), new UIPropertyMetadata(null));
147
148        public static readonly DependencyProperty LabelsProperty = LabelsKey.DependencyProperty;
149
150        private String LabelHolder
151        {
152            get { return (String)GetValue(LabelHolderProperty); }
153            set { SetValue(LabelHolderProperty, value); }
154        }
155
156        // Using a DependencyProperty as the backing store for CurrentLabel.  This enables animation, styling, binding, etc...
157        private static readonly DependencyProperty LabelHolderProperty =
158            DependencyProperty.Register("CurrentLabel", typeof(String), typeof(LabelExtractor), new UIPropertyMetadata(null));
159
160
161
162    }
163}