PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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