2

I have WPF application with target framework version as 4.5. Now I have switched to version 4.6.2.

After that, I am getting the following error in multiple places.

A TwoWay or OneWayToSource binding cannot work on the read-only property 

I am using Visual Studio 2017.

Call the project as A and it has B, C, D as dependencies. While B, C, and D are attached as DLL's I am getting the error. If they are attached as projects I am not getting the error.

Note: All B, C, D are 4.5 version projects and I am not changing them into 4.6.2 for running this project.

Can anyone update me on how to proceed further here and if there is any mistake in the way of approaching?

Update 1:

The related question is different from my case:

A TwoWay or OneWayToSource binding cannot work on the read-only property

Update 2:

The following question is similar.

A TwoWay or OneWayToSource binding cannot work on the read-only property.

but the link provided in the answer seems to be broken or redirected:

https://connect.microsoft.com/VisualStudio/feedback/details/773682/wpf-property-with-private-setter-is-updated-by-a-twoway-binding

Venkat
  • 2,549
  • 2
  • 28
  • 61
  • Are there any specific controls which throws the error? – Dipen Shah Sep 03 '18 at 07:04
  • It is an application and I am unsure that it is related to any specific controls used in the application, I can able to get the issue in multiple places. – Venkat Sep 03 '18 at 07:06
  • Did you modify any existing ViewModels? i.e did you remove property setters from viewmodels? – Dipen Shah Sep 03 '18 at 07:09
  • I have no code changes, The project is working fine in 4.5. I have just changed the framework version and I got the error. – Venkat Sep 03 '18 at 07:22
  • @DipenShah: FYI – Venkat Sep 03 '18 at 07:22
  • 1
    After following the links you posted, I found that there was a bug in .Net 4.5 which will not raise an error when you are using two-way binding for properties .with private setters. This was fixed in .Net 4.7. I don't think there is easy fix available for this except making your private setters public. – Dipen Shah Sep 03 '18 at 07:47
  • @DipenShah: Thanks for the comment, but the problem does not occur while attaching projects instead of DLL's . Any idea on this. – Venkat Sep 03 '18 at 08:38
  • Um...not sure what your mean, it 'll throw error on run time. – Dipen Shah Sep 03 '18 at 08:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179303/discussion-between-venkat-and-dipen-shah). – Venkat Sep 03 '18 at 08:42

1 Answers1

3

I know it's year late, but if anyone runs into this, it's known problem. After framework upgrade, parts of my UI started throwing InvalidOperationException. The fix was easy, just add explicit Mode=OneWay to the binding. But I still needed to know why it started if nobody changed anything in code for quite some time.

I found the solution here: https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/retargeting/4.5-4.6.1#two-way-data-binding-to-a-property-with-a-non-public-setter-is-not-supported

Attempting to data bind to a property without a public setter has never been a supported scenario. Beginning in the .NET Framework 4.5.1, this scenario will throw an InvalidOperationException. Note that this new exception will only be thrown for apps that specifically target the .NET Framework 4.5.1. If an app targets the .NET Framework 4.5, the call will be allowed. If the app does not target a particular .NET Framework version, the binding will be treated as one-way.

The problem was always there, it only just started throwing exception when it happens.

Josef Jura
  • 522
  • 6
  • 22
  • I had the exact same issue today, i updated from targeting 4.5 to 4.7.2, however the exception is only thrown when running the executable normally. If i run it with the debugger attached nothing is thrown and the application runs normally...imagine my surprise when compiling the release, running from VS, everything is alright and then deploying to the customer and bam, i get error reports... – user1464603 Jul 04 '20 at 07:41