@@ -8,87 +8,38 @@ namespace CommunityToolkit.WinUI.Controls;
88/// Allows raise an event when the value of a dependency property changes when a view model is otherwise not necessary.
99/// </summary>
1010/// <typeparam name="TPropertyType">Type of the DependencyProperty</typeparam>
11- internal partial class PropertyChangeEventSource < TPropertyType > : FrameworkElement
11+ internal partial class PropertyChangeEventSource < TPropertyType >
1212{
1313 private readonly DependencyObject _source ;
14+ private readonly DependencyProperty _property ;
15+ private readonly long _token ;
1416
1517 /// <summary>
1618 /// Occurs when the value changes.
1719 /// </summary>
1820 public event EventHandler < TPropertyType > ValueChanged ;
1921
20- /// <summary>
21- /// Value Dependency Property
22- /// </summary>
23- public static readonly DependencyProperty ValueProperty =
24- DependencyProperty . Register (
25- "Value" ,
26- typeof ( TPropertyType ) ,
27- typeof ( PropertyChangeEventSource < TPropertyType > ) ,
28- new PropertyMetadata ( default ( TPropertyType ) , OnValueChanged ) ) ;
29-
30- /// <summary>
31- /// Gets or sets the Value property. This dependency property
32- /// indicates the value.
33- /// </summary>
34- public TPropertyType Value
35- {
36- get { return ( TPropertyType ) GetValue ( ValueProperty ) ; }
37- set { SetValue ( ValueProperty , value ) ; }
38- }
39-
40- /// <summary>
41- /// Handles changes to the Value property.
42- /// </summary>
43- /// <param name="d">
44- /// The <see cref="DependencyObject"/> on which the property has changed value.
45- /// </param>
46- /// <param name="e">
47- /// Event data that is issued by any event that
48- /// tracks changes to the effective value of this property.
49- /// </param>
50- private static void OnValueChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
51- {
52- var target = ( PropertyChangeEventSource < TPropertyType > ) d ;
53- TPropertyType oldValue = ( TPropertyType ) e . OldValue ;
54- TPropertyType newValue = target . Value ;
55- target . OnValueChanged ( oldValue , newValue ) ;
56- }
57-
58- /// <summary>
59- /// Provides derived classes an opportunity to handle changes to the Value property.
60- /// </summary>
61- /// <param name="oldValue">The old Value value</param>
62- /// <param name="newValue">The new Value value</param>
63- private void OnValueChanged ( TPropertyType oldValue , TPropertyType newValue )
64- {
65- ValueChanged ? . Invoke ( _source , newValue ) ;
66- }
67-
6822 /// <summary>
6923 /// Initializes a new instance of the <see cref="PropertyChangeEventSource{TPropertyType}"/> class.
7024 /// </summary>
7125 /// <param name="source">The source.</param>
72- /// <param name="propertyName">Name of the property.</param>
73- /// <param name="bindingMode">The binding mode.</param>
26+ /// <param name="property">Monitor this dependency property for changes.</param>
7427#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
7528 public PropertyChangeEventSource (
7629#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
7730 DependencyObject source ,
78- string propertyName ,
79- BindingMode bindingMode = BindingMode . TwoWay )
31+ DependencyProperty property )
8032 {
8133 _source = source ;
34+ _property = property ;
35+ _token = source . RegisterPropertyChangedCallback ( property , ( _ , _ ) =>
36+ {
37+ ValueChanged ? . Invoke ( this , ( TPropertyType ) source . GetValue ( property ) ) ;
38+ } ) ;
39+ }
8240
83- // Bind to the property to be able to get its changes relayed as events through the ValueChanged event.
84- var binding =
85- new Binding
86- {
87- Source = source ,
88- Path = new PropertyPath ( propertyName ) ,
89- Mode = bindingMode
90- } ;
91-
92- SetBinding ( ValueProperty , binding ) ;
41+ public void Unregister ( )
42+ {
43+ _source . UnregisterPropertyChangedCallback ( _property , _token ) ;
9344 }
9445}
0 commit comments