Valerie
Valerie

Reputation: 1215

WPF .Net 4 - OneWayToSource binding to write-only property works on some machines! How?

I recently added a OneWayToSource binding in our code to a read-only property in a view model. At the time I did not know about the change in .Net 4, that this will cause an exception when there is no getter:

<Popup IsOpen="{Binding IsPopupOpen, Mode=OneWayToSource}" />
...
public bool IsPopupOpen
{
    set
    {
        // do something with value
    }
}

The problem is, this code works on my computer without throwing an exception. The exact same executable does not work on other machines - the popup does not open, and the expected exception gets thrown. I cannot figure out how this is possible? The project targets .Net FW 4.

I made sure to clear out my bin folder and rebuild, but it is still working without exception on my machine, and not on others.

I know how to "fix" this - simply add a getter to the property. However I need to figure out how the program is functioning differently on different machines, as this could cause other serious issues...

EDIT:

This is driving me mad! I look at my popup using Snoop WPF utility, and there appears to be no binding to IsOpen at all (the binding is a new addition to the XAML). However, if I attach the debugger to the app and set a breakpoint inside the setter of the bound property, it hits my breakpoint!!

Upvotes: 3

Views: 2687

Answers (1)

Pavel Voronin
Pavel Voronin

Reputation: 13983

A bit of guessing.

There's plausible that after this change in net 4.0 they decided to make it more consistent in .net 4.5: if getter exists property is reread back to UI, otherwise nothing terrible happens and no exception is thrown.

If so and you have .net 4.5 installed you can get this behavior instead of the expected one.
We've met cases when updates to 4.5 propagated to 4.0 silently.

Upvotes: 2

Related Questions