Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe to a DependencyProperty Change Notification

Tags:

c#

wpf

I have a Static Dependency Property and I need to know when its value changes so I can call a callback and update a value else where. Right now I can't do it because the callback isn't static and the Dependency change event is.

I have it currently working when the LostFocus event fires, but I'd prefer to have it wired up to whenever a change occurs.

like image 633
twreid Avatar asked Aug 10 '12 17:08

twreid


1 Answers

The dependency property change notification passes in the object. You can use that to map to a non-static variable:

static void OnThePropChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
     YourClass instance = (YourClass)obj;
     instance.ThePropChanged(args); // Call non-static

     // Alternatively, you can just call the callback directly:
     // instance.CallbackMethod(...)
}

// This is a non-static version of the dep. property changed event
void ThePropChanged(DependencyPropertyChangedEventArgs args)
{
      // Raise your callback here... 
}
like image 163
Reed Copsey Avatar answered Nov 15 '22 23:11

Reed Copsey