PageRenderTime 90ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/V4/PrismLibrary/Phone/Prism.Interactivity/DependencyPropertyListener.cs

#
C# | 94 lines | 47 code | 9 blank | 38 comment | 6 complexity | cc9a3f4723f6175b09d71bbbe63058d1 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.Data;
  20. namespace Microsoft.Practices.Prism.Interactivity
  21. {
  22. /// <summary>
  23. /// Registers a new dependency property for tracking data and provides
  24. /// notification on data changes.
  25. /// </summary>
  26. public class DependencyPropertyListener
  27. {
  28. private readonly DependencyProperty property;
  29. private static int index;
  30. private FrameworkElement target;
  31. /// <summary>
  32. /// Instantiates a new <see cref="DependencyPropertyListener"/>.
  33. /// </summary>
  34. /// <remarks>
  35. /// This registers creates an attached property with the name starting DependencyPropertyListener. This
  36. /// attached property set on a <see cref="FrameworkElement"/> when <see cref="Attach"/> is called.</remarks>
  37. public DependencyPropertyListener()
  38. {
  39. this.property =
  40. DependencyProperty.RegisterAttached(
  41. "DependencyPropertyListener" + index++,
  42. typeof(object),
  43. typeof(DependencyPropertyListener),
  44. new PropertyMetadata(null, this.HandleValueChanged));
  45. }
  46. /// <summary>
  47. /// This event is raised when the attached property value changes.
  48. /// </summary>
  49. public event EventHandler<BindingChangedEventArgs> Changed;
  50. /// <summary>
  51. /// Attaches a <see cref="DependencyProperty"/> to a framework element with
  52. /// the provided <see cref="Binding"/>.
  53. /// </summary>
  54. /// <param name="element">The <see cref="FrameworkElement"/> to attach the monitoring dependency property to.</param>
  55. /// <param name="binding">The binding to use with the attached property.</param>
  56. public void Attach(FrameworkElement element, Binding binding)
  57. {
  58. if (element == null)
  59. {
  60. throw new ArgumentNullException("element");
  61. }
  62. if (this.target != null)
  63. {
  64. throw new InvalidOperationException("Cannot attach an already attached listener");
  65. }
  66. this.target = element;
  67. this.target.SetBinding(this.property, binding);
  68. }
  69. ///<summary>
  70. /// Detaches binding listener from target.
  71. ///</summary>
  72. public void Detach()
  73. {
  74. this.target.ClearValue(this.property);
  75. this.target = null;
  76. }
  77. private void HandleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  78. {
  79. if (this.Changed != null)
  80. {
  81. this.Changed(this, new BindingChangedEventArgs(e));
  82. }
  83. }
  84. }
  85. }