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