annotate MetroWpf/MetroWpf.Xaml/Events/WeakPropertyChangeNotifier.cs @ 100:9b6855dc165e

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