PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/Quickstarts/State-Based Navigation/State-Based Navigation/Infrastructure/Behaviors/RelocatePopupBehavior.cs

#
C# | 115 lines | 80 code | 16 blank | 19 comment | 14 complexity | b4dfd41b12dd7df475f3d1a6359d3f62 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;
 18using System.Windows;
 19using System.Windows.Controls.Primitives;
 20using System.Windows.Interactivity;
 21
 22namespace StateBasedNavigation.Infrastructure.Behaviors
 23{
 24    /// <summary>
 25    /// Behavior that ensures a popup is located at the bottom-right corner of its parent.
 26    /// </summary>
 27    public class RelocatePopupBehavior : Behavior<Popup>
 28    {
 29        protected override void OnAttached()
 30        {
 31            base.OnAttached();
 32
 33            this.AssociatedObject.Opened += this.OnPopupOpened;
 34            this.AssociatedObject.Closed += this.OnPopupClosed;
 35        }
 36
 37        protected override void OnDetaching()
 38        {
 39            this.AssociatedObject.Opened -= this.OnPopupOpened;
 40            this.AssociatedObject.Closed -= this.OnPopupClosed;
 41
 42            this.DetachSizeChangeHandlers();
 43
 44            base.OnDetaching();
 45        }
 46
 47        private void OnPopupOpened(object sender, EventArgs e)
 48        {
 49            this.UpdatePopupOffsets();
 50            this.AttachSizeChangeHandlers();
 51        }
 52
 53        private void OnPopupClosed(object sender, EventArgs e)
 54        {
 55            this.DetachSizeChangeHandlers();
 56        }
 57
 58        private void AttachSizeChangeHandlers()
 59        {
 60            var child = this.AssociatedObject.Child as FrameworkElement;
 61            if (child != null)
 62            {
 63                child.SizeChanged += this.OnChildSizeChanged;
 64            }
 65
 66            var parent = this.AssociatedObject.Parent as FrameworkElement;
 67            if (parent != null)
 68            {
 69                parent.SizeChanged += this.OnParentSizeChanged;
 70            }
 71        }
 72
 73        private void DetachSizeChangeHandlers()
 74        {
 75            var child = this.AssociatedObject.Child as FrameworkElement;
 76            if (child != null)
 77            {
 78                child.SizeChanged -= this.OnChildSizeChanged;
 79            }
 80
 81            var parent = this.AssociatedObject.Parent as FrameworkElement;
 82            if (parent != null)
 83            {
 84                parent.SizeChanged -= this.OnParentSizeChanged;
 85            }
 86        }
 87
 88        private void OnChildSizeChanged(object sender, EventArgs e)
 89        {
 90            this.UpdatePopupOffsets();
 91        }
 92
 93        private void OnParentSizeChanged(object sender, EventArgs e)
 94        {
 95            this.UpdatePopupOffsets();
 96        }
 97
 98        private void UpdatePopupOffsets()
 99        {
100            if (this.AssociatedObject != null)
101            {
102                var child = this.AssociatedObject.Child as FrameworkElement;
103                var parent = this.AssociatedObject.Parent as FrameworkElement;
104
105                if (child != null && parent != null)
106                {
107                    var anchor = new Point(parent.ActualWidth, parent.ActualHeight);
108
109                    this.AssociatedObject.HorizontalOffset = anchor.X - child.ActualWidth;
110                    this.AssociatedObject.VerticalOffset = anchor.Y - child.ActualHeight;
111                }
112            }
113        }
114    }
115}