PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/StockTrader RI/Desktop/StockTraderRI.Infrastructure/Behaviors/DialogActivationBehavior.cs

#
C# | 106 lines | 58 code | 10 blank | 38 comment | 7 complexity | 1900007e3bab8389ba150847c5841061 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.Specialized;
  18. using System.Windows;
  19. using Microsoft.Practices.Prism.Regions;
  20. using Microsoft.Practices.Prism.Regions.Behaviors;
  21. namespace StockTraderRI.Infrastructure.Behaviors
  22. {
  23. /// <summary>
  24. /// Defines a behavior that creates a Dialog to display the active view of the target <see cref="IRegion"/>.
  25. /// </summary>
  26. public abstract class DialogActivationBehavior : RegionBehavior, IHostAwareRegionBehavior
  27. {
  28. /// <summary>
  29. /// The key of this behavior
  30. /// </summary>
  31. public const string BehaviorKey = "DialogActivation";
  32. private IWindow contentDialog;
  33. /// <summary>
  34. /// Gets or sets the <see cref="DependencyObject"/> that the <see cref="IRegion"/> is attached to.
  35. /// </summary>
  36. /// <value>A <see cref="DependencyObject"/> that the <see cref="IRegion"/> is attached to.
  37. /// This is usually a <see cref="FrameworkElement"/> that is part of the tree.</value>
  38. public DependencyObject HostControl { get; set; }
  39. /// <summary>
  40. /// Performs the logic after the behavior has been attached.
  41. /// </summary>
  42. protected override void OnAttach()
  43. {
  44. this.Region.ActiveViews.CollectionChanged += this.ActiveViews_CollectionChanged;
  45. }
  46. /// <summary>
  47. /// Override this method to create an instance of the <see cref="IWindow"/> that
  48. /// will be shown when a view is activated.
  49. /// </summary>
  50. /// <returns>
  51. /// An instance of <see cref="IWindow"/> that will be shown when a
  52. /// view is activated on the target <see cref="IRegion"/>.
  53. /// </returns>
  54. protected abstract IWindow CreateWindow();
  55. private void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  56. {
  57. if (e.Action == NotifyCollectionChangedAction.Add)
  58. {
  59. this.CloseContentDialog();
  60. this.PrepareContentDialog(e.NewItems[0]);
  61. }
  62. else if (e.Action == NotifyCollectionChangedAction.Remove)
  63. {
  64. this.CloseContentDialog();
  65. }
  66. }
  67. private Style GetStyleForView()
  68. {
  69. return this.HostControl.GetValue(RegionPopupBehaviors.ContainerWindowStyleProperty) as Style;
  70. }
  71. private void PrepareContentDialog(object view)
  72. {
  73. this.contentDialog = this.CreateWindow();
  74. this.contentDialog.Content = view;
  75. this.contentDialog.Owner = this.HostControl;
  76. this.contentDialog.Closed += this.ContentDialogClosed;
  77. this.contentDialog.Style = this.GetStyleForView();
  78. this.contentDialog.Show();
  79. }
  80. private void CloseContentDialog()
  81. {
  82. if (this.contentDialog != null)
  83. {
  84. this.contentDialog.Closed -= this.ContentDialogClosed;
  85. this.contentDialog.Close();
  86. this.contentDialog.Content = null;
  87. this.contentDialog.Owner = null;
  88. }
  89. }
  90. private void ContentDialogClosed(object sender, System.EventArgs e)
  91. {
  92. this.Region.Deactivate(this.contentDialog.Content);
  93. this.CloseContentDialog();
  94. }
  95. }
  96. }