Home > C#, Silverlight, Windows Phone 7.5, WP7 > Listening to a Dependency Property’s Change notification

Listening to a Dependency Property’s Change notification

I was digging a bit around Silverlight Dependency properties, mainly to see how to receive change notification when ever a dependency property is changed. In WPF, this is straight forward, you may use the DependencyPropertyDescriptor, and call AddValueChanged. Like this.

  1. DependencyPropertyDescriptor desc =
  2.     DependencyPropertyDescriptor.FromProperty
  3.     (UIElement.VisibilityProperty, typeof(UIElement));
  4. desc.AddValueChanged(this.myLabel, new EventHandler(VisibilityChanged));

Now, how to do this in Silverlight? I roamed around a bit, but can’t really find a good way of doing that. So, here is a quick hack. The trick is to use DependencyProperty.RegisterAttached(..) to instantiate a dependency property type (Ahem). This is what we are doing below.

  • Create a binding with the FrameworkElement as the source
  • Use DependencyProperty.RegisterAttached(..) to create a DependencyProperty instance
  • Use FrameworkElement.SetBinding(dp, binding) to associate the dependency property with the element, via the binding.

So, we’ll get a call back when ever the dependency property changes. The RegisterForNotification summarizes what I explained above.

  1. /// Listen for change of the dependency property  
  2. public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
  3. {
  4.     //Bind to a depedency property  
  5.     Binding b = new Binding(propertyName) { Source = element };
  6.     var prop = System.Windows.DependencyProperty.RegisterAttached(
  7.         “ListenAttached”+propertyName,
  8.         typeof(object),
  9.         typeof(UserControl),
  10.         new System.Windows.PropertyMetadata(callback));
  11.     element.SetBinding(prop, b);
  12. }

Create a new Silverlight project, and add the above code to the code behind. And add the following controls to your xaml page.

  1. <Grid x:Name=“LayoutRoot” Background=“White”>
  2.      <TextBox Text=“hello” x:Name=“txtMain” />
  3.      <Slider Value=“10” Minimum=“0” Maximum=“100” x:Name=“sliderMain”/>
  4.  </Grid>

Now, just call RegisterForNotification from where ever you need (I’ve it in the Constructor of my MainPage.cs, just under the InitializeComponent() call), like

  1. //Shows a message box when the text of Textbox or value of Slider changes.  
  2. RegisterForNotification
  3.     (“Text”, this.txtMain, (d, e) => MessageBox.Show(“Text changed”));
  4. RegisterForNotification
  5.     (“Value”, this.sliderMain, (d, e) => MessageBox.Show(“Value changed”));

Fire up your project, and you’ll see the message boxes when ever the value change happens. So, now it looks pretty interesting, I guess. Opens up possibilities like having Data triggers in Silverlight, probably via an attached behaviour. Need to evaluate the pros and cons of this approach, came across this scenario while doing some other hacks.

Original blog post by Anoop Madhusudanan: http://www.amazedsaint.com/2009/12/silverlight-listening-to-dependency.html

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: