A Silverlight helper class to get/set Dependency property values of other objects

By Anoop Madhusudanan

Vote on HN

In my last post on Dependency Properties (Read here), I explained how to  listen for dependency property change notification of a UI Element. image

Here is a more ‘refined’ extension method - so that you can listen to Property change notifications of a source - by creating and registering a ‘listener’ dependency property in your class ('owner') and setting up a binding with the ‘source’ object. Remember that starting from Silverlight 4.0, you can even have dependency objects as the source.

This is useful when you create your own Silverlight Behaviors, triggers or actions. For example, you may want to listen to the property change notification of an element - to Invoke your trigger when the property value of the ‘source’ changes.

 

    public static class DependencyPropertyHelper 
    {
        /// <summary>
        /// Listen for change of a property of the source, 
        /// and callback the same to the context via the callback
        /// </summary>

        public static System.Windows.DependencyProperty RegisterForNotification
            (this DependencyObject owner, object source,
            string propertyPath, PropertyChangedCallback callback)
        {

            return RegisterForNotification(owner, source, propertyPath, callback, false);

        }

        /// <summary>
        /// Listen for change of a property of the source, 
        /// and callback the same to the context via the callback.
        /// Creates a two way binding if you specify 'twoway' to true
        /// </summary>
        /// <param name="propertyPath"></param>
        /// <param name="element"></param>
        public static System.Windows.DependencyProperty RegisterForNotification
            (this DependencyObject owner, object source,
            string propertyPath, PropertyChangedCallback callback, bool twoWay)
        {

            //Bind to a depedency property
            Binding b = new Binding(propertyPath) { Source = source, Mode = BindingMode.OneWay };
            if (twoWay) b.Mode = BindingMode.TwoWay;

            var prop = System.Windows.DependencyProperty.RegisterAttached(
                "Listener" + propertyPath + DateTime.Now.Ticks,
                typeof(object),
                owner.GetType(),
                new System.Windows.PropertyMetadata(callback));

            BindingOperations.SetBinding(owner, prop, b);

            return prop;

        }
    }

And now, from your 'owner' class (say, from your trigger or behavior class), you can set up a binding to a source like this.

//Register for notification some where
this.RegisterForNotification
    (Source, this.Property, OnPropertyValueChanged);

//...

/// Callback invoked when the property changes
private void OnListenAttachedPropertyChanged(DependencyObject dependencyObject,
      DependencyPropertyChangedEventArgs eventArgs)
   {
           //Do stuff here
   }

If you want to update the source property value to which you are binding, you might want specify that the twoway parameter is true when invoking RegisterForNotification - and keep the dependency property created for updating the value, later like

targetDP =this.RegisterForNotification
              (Target, this.Property, OnPropertyValueChanged, true);


//Later, set the value some where
 SetValue(targetDP, Value);

 

You may find the DependencyPropertyHelper class and a related PropertyChangedTrigger implementation here in Slex project - http://slex.codeplex.com

Shout it
© 2012. All Rights Reserved. Amazedsaint.com