PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

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