comparison MetroWpf/MetroWpf.Xaml/Events/WeakPropertyChangeNotifier.cs @ 17:7721a1270363

New files added during dev
author stevenh7776 stevenhollidge@hotmail.com
date Wed, 14 Mar 2012 01:23:57 +0700
parents
children
comparison
equal deleted inserted replaced
16:2395908fa45b 17:7721a1270363
1 using System;
2 using System.ComponentModel;
3 using System.Windows;
4 using System.Windows.Data;
5
6 namespace MetroWpf.Xaml.Events
7 {
8 /// <summary>
9 /// http://agsmith.wordpress.com/2008/04/07/propertydescriptor-addvaluechanged-alternative/
10 /// </summary>
11 public sealed class WeakPropertyChangeNotifier
12 : DependencyObject, IDisposable
13 {
14 #region Member Variables
15 private readonly WeakReference _propertySource;
16 #endregion // Member Variables
17
18 #region Constructor
19 public WeakPropertyChangeNotifier(DependencyObject propertySource, string path)
20 : this(propertySource, new PropertyPath(path))
21 {
22 }
23 public WeakPropertyChangeNotifier(DependencyObject propertySource, DependencyProperty property)
24 : this(propertySource, new PropertyPath(property))
25 {
26 }
27 public WeakPropertyChangeNotifier(DependencyObject propertySource, PropertyPath property)
28 {
29 if (null == propertySource)
30 throw new ArgumentNullException("propertySource");
31 if (null == property)
32 throw new ArgumentNullException("property");
33
34 this._propertySource = new WeakReference(propertySource);
35
36 var binding = new System.Windows.Data.Binding();
37 binding.Path = property;
38 binding.Mode = BindingMode.OneWay;
39 binding.Source = propertySource;
40 BindingOperations.SetBinding(this, ValueProperty, binding);
41 }
42 #endregion // Constructor
43
44 #region PropertySource
45 public DependencyObject PropertySource
46 {
47 get
48 {
49 try
50 {
51 // note, it is possible that accessing the target property
52 // will result in an exception so i’ve wrapped this check
53 // in a try catch
54 return this._propertySource.IsAlive
55 ? this._propertySource.Target as DependencyObject
56 : null;
57 }
58 catch
59 {
60 return null;
61 }
62 }
63 }
64 #endregion // PropertySource
65
66 #region Value
67 /// <summary>
68 /// Identifies the <see cref="Value"/> dependency property
69 /// </summary>
70 public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
71 typeof(object), typeof(WeakPropertyChangeNotifier), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnPropertyChanged)));
72
73 private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
74 {
75 WeakPropertyChangeNotifier notifier = (WeakPropertyChangeNotifier)d;
76 if (null != notifier.ValueChanged)
77 notifier.ValueChanged(notifier, EventArgs.Empty);
78 }
79
80 /// <summary>
81 /// Returns/sets the value of the property
82 /// </summary>
83 /// <seealso cref="ValueProperty"/>
84 [Description("Returns/sets the value of the property")]
85 [Category("Behavior")]
86 [Bindable(true)]
87 public object Value
88 {
89 get
90 {
91 return (object)this.GetValue(ValueProperty);
92 }
93 set
94 {
95 this.SetValue(ValueProperty, value);
96 }
97 }
98 #endregion //Value
99
100 #region Events
101 public event EventHandler ValueChanged;
102 #endregion // Events
103
104 #region IDisposable Members
105 public void Dispose()
106 {
107 BindingOperations.ClearBinding(this, ValueProperty);
108 }
109 #endregion
110 }
111 }