PageRenderTime 97ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/V2.2/trunk/CAL/Desktop/Composite.Presentation/Regions/Behaviors/BindRegionContextToDependencyObjectBehavior.cs

#
C# | 122 lines | 84 code | 9 blank | 29 comment | 17 complexity | 2d5f32e4446e14c046fe48305bfa1f1c 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;
  18. using System.Collections.Specialized;
  19. using System.ComponentModel;
  20. using System.Windows;
  21. using Microsoft.Practices.Composite.Regions;
  22. namespace Microsoft.Practices.Composite.Presentation.Regions.Behaviors
  23. {
  24. /// <summary>
  25. /// Defines a behavior that forwards the <see cref="RegionManager.RegionContextProperty"/>
  26. /// to the views in the region.
  27. /// </summary>
  28. public class BindRegionContextToDependencyObjectBehavior : IRegionBehavior
  29. {
  30. /// <summary>
  31. /// The key of this behavior.
  32. /// </summary>
  33. public const string BehaviorKey = "ContextToDependencyObject";
  34. /// <summary>
  35. /// Behavior's attached region.
  36. /// </summary>
  37. public IRegion Region { get; set; }
  38. /// <summary>
  39. /// Attaches the behavior to the specified region.
  40. /// </summary>
  41. public void Attach()
  42. {
  43. this.Region.Views.CollectionChanged += this.Views_CollectionChanged;
  44. this.Region.PropertyChanged += this.Region_PropertyChanged;
  45. SetContextToViews(this.Region.Views, this.Region.Context);
  46. this.AttachNotifyChangeEvent(this.Region.Views);
  47. }
  48. private static void SetContextToViews(IEnumerable views, object context)
  49. {
  50. foreach (var view in views)
  51. {
  52. DependencyObject dependencyObjectView = view as DependencyObject;
  53. if (dependencyObjectView != null)
  54. {
  55. ObservableObject<object> contextWrapper = RegionContext.GetObservableContext(dependencyObjectView);
  56. contextWrapper.Value = context;
  57. }
  58. }
  59. }
  60. private void AttachNotifyChangeEvent(IEnumerable views)
  61. {
  62. foreach (var view in views)
  63. {
  64. var dependencyObject = view as DependencyObject;
  65. if (dependencyObject != null)
  66. {
  67. ObservableObject<object> viewRegionContext = RegionContext.GetObservableContext(dependencyObject);
  68. viewRegionContext.PropertyChanged += this.ViewRegionContext_OnPropertyChangedEvent;
  69. }
  70. }
  71. }
  72. private void DetachNotifyChangeEvent(IEnumerable views)
  73. {
  74. foreach (var view in views)
  75. {
  76. var dependencyObject = view as DependencyObject;
  77. if (dependencyObject != null)
  78. {
  79. ObservableObject<object> viewRegionContext = RegionContext.GetObservableContext(dependencyObject);
  80. viewRegionContext.PropertyChanged -= this.ViewRegionContext_OnPropertyChangedEvent;
  81. }
  82. }
  83. }
  84. private void ViewRegionContext_OnPropertyChangedEvent(object sender, PropertyChangedEventArgs args)
  85. {
  86. if (args.PropertyName == "Value")
  87. {
  88. var context = (ObservableObject<object>) sender;
  89. this.Region.Context = context.Value;
  90. }
  91. }
  92. private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  93. {
  94. if (e.Action == NotifyCollectionChangedAction.Add)
  95. {
  96. SetContextToViews(e.NewItems, this.Region.Context);
  97. this.AttachNotifyChangeEvent(e.NewItems);
  98. }
  99. else if (e.Action == NotifyCollectionChangedAction.Remove && this.Region.Context != null)
  100. {
  101. SetContextToViews(e.OldItems, null);
  102. this.DetachNotifyChangeEvent(e.OldItems);
  103. }
  104. }
  105. private void Region_PropertyChanged(object sender, PropertyChangedEventArgs e)
  106. {
  107. if (e.PropertyName == "Context")
  108. {
  109. SetContextToViews(this.Region.Views, this.Region.Context);
  110. }
  111. }
  112. }
  113. }