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