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