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