PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/V2/trunk/RI/Desktop/StockTraderRI.Infrastructure/Behaviors/RegionPopupBehaviors.cs

#
C# | 139 lines | 61 code | 11 blank | 67 comment | 7 complexity | 2b036633257fcced109714e8123f919e 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.ComponentModel;
 18using System.Windows;
 19using Microsoft.Practices.Composite.Presentation.Regions;
 20using Microsoft.Practices.Composite.Regions;
 21using Microsoft.Practices.ServiceLocation;
 22
 23namespace StockTraderRI.Infrastructure.Behaviors
 24{
 25    /// <summary>
 26    /// Declares the Attached Properties and Behaviors for implementing Popup regions.
 27    /// </summary>
 28    /// <remarks>
 29    /// Although the fastest way is to create a RegionAdapter for a Window and register it with the RegionAdapterMappings,
 30    /// this would be conceptually incorrect because we want to create a new popup window everytime a view is added 
 31    /// (instead of having a Window as a host control and replacing its contents everytime Views are added, as other adapters do).
 32    /// This is why we have a different class for this behavior, instead of reusing the <see cref="RegionManager.RegionNameProperty"/> attached property.
 33    /// </remarks>
 34    public static class RegionPopupBehaviors
 35    {
 36        /// <summary>
 37        /// The name of the Popup <see cref="IRegion"/>.
 38        /// </summary>
 39        public static readonly DependencyProperty CreatePopupRegionWithNameProperty =
 40            DependencyProperty.RegisterAttached("CreatePopupRegionWithName", typeof(string), typeof(RegionPopupBehaviors), new PropertyMetadata(CreatePopupRegionWithNamePropertyChanged));
 41
 42        /// <summary>
 43        /// The <see cref="Style"/> to set to the Popup.
 44        /// </summary>
 45        public static readonly DependencyProperty ContainerWindowStyleProperty =
 46          DependencyProperty.RegisterAttached("ContainerWindowStyle", typeof(Style), typeof(RegionPopupBehaviors), null);
 47
 48        /// <summary>
 49        /// Gets the name of the Popup <see cref="IRegion"/>.
 50        /// </summary>
 51        /// <param name="owner">Owner of the Popup.</param>
 52        /// <returns>The name of the Popup <see cref="IRegion"/>.</returns>
 53        public static string GetCreatePopupRegionWithName(DependencyObject owner)
 54        {
 55            return owner.GetValue(CreatePopupRegionWithNameProperty) as string;
 56        }
 57
 58        /// <summary>
 59        /// Sets the name of the Popup <see cref="IRegion"/>.
 60        /// </summary>
 61        /// <param name="owner">Owner of the Popup.</param>
 62        /// <param name="value">Name of the Popup <see cref="IRegion"/>.</param>
 63        public static void SetCreatePopupRegionWithName(DependencyObject owner, string value)
 64        {
 65            owner.SetValue(CreatePopupRegionWithNameProperty, value);
 66        }
 67
 68        /// <summary>
 69        /// Gets the <see cref="Style"/> for the Popup.
 70        /// </summary>
 71        /// <param name="owner">Owner of the Popup.</param>
 72        /// <returns>The <see cref="Style"/> for the Popup.</returns>
 73        public static Style GetContainerWindowStyle(DependencyObject owner)
 74        {
 75            return owner.GetValue(ContainerWindowStyleProperty) as Style;
 76        }
 77
 78        /// <summary>
 79        /// Sets the <see cref="Style"/> for the Popup.
 80        /// </summary>
 81        /// <param name="owner">Owner of the Popup.</param>
 82        /// <param name="style"><see cref="Style"/> for the Popup.</param>
 83        public static void SetContainerWindowStyle(DependencyObject owner, Style style)
 84        {
 85            owner.SetValue(ContainerWindowStyleProperty, style);
 86        }
 87
 88        /// <summary>
 89        /// Creates a new <see cref="IRegion"/> and registers it in the default <see cref="IRegionManager"/>
 90        /// attaching to it a <see cref="DialogActivationBehavior"/> behavior.
 91        /// </summary>
 92        /// <param name="owner">The owner of the Popup.</param>
 93        /// <param name="regionName">The name of the <see cref="IRegion"/>.</param>
 94        /// <remarks>
 95        /// This method would typically not be called directly, instead the behavior 
 96        /// should be set through the Attached Property <see cref="CreatePopupRegionWithNameProperty"/>.
 97        /// </remarks>
 98        public static void RegisterNewPopupRegion(DependencyObject owner, string regionName)
 99        {
100            // Creates a new region and registers it in the default region manager.
101            // Another option if you need the complete infrastructure with the default region behaviors
102            // is to extend DelayedRegionCreationBehavior overriding the CreateRegion method and create an 
103            // instance of it that will be in charge of registering the Region once a RegionManager is
104            // set as an attached property in the Visual Tree.
105            IRegionManager regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
106            if (regionManager != null)
107            {
108                IRegion region = new SingleActiveRegion();
109                DialogActivationBehavior behavior;
110#if SILVERLIGHT
111                behavior = new PopupDialogActivationBehavior();
112#else
113                behavior = new WindowDialogActivationBehavior();
114#endif
115                behavior.HostControl = owner;
116
117                region.Behaviors.Add(DialogActivationBehavior.BehaviorKey, behavior);
118                regionManager.Regions.Add(regionName, region);
119            }
120        }
121
122        private static void CreatePopupRegionWithNamePropertyChanged(DependencyObject hostControl, DependencyPropertyChangedEventArgs e)
123        {
124            if (IsInDesignMode(hostControl))
125            {
126                return;
127            }
128
129            RegisterNewPopupRegion(hostControl, e.NewValue as string);
130        }
131
132        private static bool IsInDesignMode(DependencyObject element)
133        {
134            // Due to a known issue in Cider, GetIsInDesignMode attached property value is not enough to know if it's in design mode.
135            return DesignerProperties.GetIsInDesignMode(element) || Application.Current == null
136                   || Application.Current.GetType() == typeof(Application);
137        }
138    }
139}