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