Getting 'this' pointer inside dependency property changed callback

Posted by mizipzor on Stack Overflow See other posts from Stack Overflow or by mizipzor
Published on 2010-03-16T09:18:07Z Indexed on 2010/03/16 9:46 UTC
Read the original article Hit count: 265

Filed under:
|

I have the following dependency property inside a class:

class FooHolder
{
    public static DependencyProperty CurrentFooProperty = DependencyProperty.Register(
        "CurrentFoo",
        typeof(Foo), 
        typeof(FooHandler),
        new PropertyMetadata(OnCurrentFooChanged));

    private static void OnCurrentFooChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FooHolder holder = (FooHolder) d.Property.Owner; // <- something like this

        // do stuff with holder
    }
}

I need to be able to retrieve a reference to the class instance in which the changed property belongs.

This is since FooHolder has some event handlers that needs to be hooked/unhooked when the value of the property is changed. The property changed callback must be static, but the event handler is not.

© Stack Overflow or respective owner

Related posts about c#

Related posts about dependency-properties