C# function called after dependency property have been set

Posted by mizipzor on Stack Overflow See other posts from Stack Overflow or by mizipzor
Published on 2010-03-15T16:39:06Z Indexed on 2010/03/15 16:39 UTC
Read the original article Hit count: 240

My code currently looks like this:

private Foo myFoo;

public Foo CurrentFoo
{
    get { return myFoo; }
    set { SetFoo(value); }
}

private void SetFoo(Foo newFoo)
{
    // Do stuff
    // Here be dragons

    myFoo = newFoo;
}

To be able to bind it in XAML/WPF I need to turn Foo into a dependency property:

public static DependencyProperty CurrentFooProperty = 
    DependencyProperty.Register("CurrentFoo", typeof(Foo), 
        typeof(FooHandler), new PropertyMetadata(false));        

public Foo CurrentFoo
{
    get { return (Foo)GetValue(CurrentFooProperty); }
    set { SetValue(CurrentFooProperty, value); }
}

Ive heard that you shouldnt do magic inside the actual C# property set {}, since it might not be called but the value is written directly to the dependency property. If this is false, let me know, it seems like the most obvious and simple route to take.

I know I can add a validation function to the dependency property but I assume that it shouldnt be used for this? I need to communicate the change to legacy systems that cannot yet be bound in XAML.

Whats the best way to approach this problem?

© Stack Overflow or respective owner

Related posts about c#

Related posts about xaml