Today I rebuilt a WPF application - .NET Framework 4.8 - and our tester came back to me saying that an error was being raised:
A TwoWay or OneWayToSource binding cannot work on the read-only property
The error itself is not a problem. I immediately recognised it for what it was, went to the problematic Binding, added Mode=OneWay and the problem went away.
But here's the problem
The original binding was already in the code. Just on a different control. It didn't cause a problem there... but if the property it's binding to is ReadOnly, shouldn't it have caused a problem wherever it's used?
The reason this bothers me so much is that our tester also reported that before the error was raised the application seemed "slow". So it occurred to me that using TwoWay bindings where OneWay bindings should be used, even if WPF is somehow catching and handling the error, is causing a performance impact. If that is the case I need to take action.
My questions therefore are:
Does WPF sometimes handle TwoWay binding errors so that the application continues to work even if the TwoWay binding is inappropriate
Can using TwoWay binding inappropriately cause a significant performance impact and should I address it?
Does WPF sometimes handle TwoWay binding errors so that the application continues to work even if the TwoWay binding is inappropriate
It did before .NET Framework 4.5.1. Since this update, it throws an InvalidOperationException saying exactly "A TwoWay or OneWayToSource binding cannot work on the read-only property".
Can using TwoWay binding inappropriately cause a significant performance impact and should I address it?
No. But you should obviously avoid TwoWay bind to read-only properties to avoid getting the exception.
Related
My application is working fine, all of a sudden while loading a designer in wpf form, im getting errors.
Object reference not set to instance of an object.
As if it is in a loop. After pressing the enter button for sometime. Im getting this error.
Microsoft Visual Studio XAML UI Designer has stopped working.
And after this, im getting this.
System.Runtime.Remoting.RemotingException
[9980] Designer process terminated unexpectedly!
The number in square brackets is changing evrytime.
Im using Visual Studio 2012. Im not yet running the application, Im just switching to designer mode from code behind. If I compile and run the application, it runs fine.
Kindly help.
Edit:
Here are the three errors in single image. (I cant post morethan one link)
xamlerrors
There's an error either in the code-behind or DataContext (if you're using a ViewModel). After you fix that error, click on Click here to reload the designer. What's happening is that the designer is trying to load everything up during design time and since there's an error, it's unable to, as if the program was actually running. That null reference exception can be a bit misleading in terms of trying to figure out the cause, because it'll be thrown if you have an error in your code-behind or ViewModel.
Things to check:
Is your View referencing the correct ViewModel?
Is your code-behind portion of the View matching the View name? Some people copy their Views, but forget to change the Class name in the code-behind.
Do you have any errors in your ViewModel?
What about models? If they're loaded during design time and contain errors, this could cause the above exception.
I noticed that those kind of errors are almost everytime caused by
an error in the parameterless constructor of the view
an error in the constructor of the view model which is attached to the view
Keep in mind that when the designer loads, it calls the parameterless constructor of the view (there has to be one for the designer!). If there are "complex things" done there which can only performed correctly at runtime, there will likely be errors at design time.
The same is true for the constructor of the view model which is called from the view.
Check whether design mode is active
For example you should not load data from a repository in a constructor.
If you do it in the constructor, at least check whether the design mode is active or not like explained in this answer here about the GetIsInDesignMode attached property and only do the complex logic in the constructor if the design mode is not active.
Debugging the problem
You also have the possiblity to debug the problem. In order to do this, you have to open a second instance of Visual Studio and debug the designer process of your original Visual Studio instance from there.
The process is described in detail here: Debugging an Exception (only) occurring in design view
After upgrading a project written in VS2012 against .NET 4.5 to VS2013 .NET 4.5.1 I the binding to my checkboxes throw an invalidoperation exception: A TwoWay or OneWayToSource binding cannot work on the read-only property
<CheckBox IsHitTestVisible="False" Focusable="False" Content="Invert TXD" IsChecked="{Binding EepromDataModel.InvertTxd}"/>
After adding the correct mode (OneWay) it runs fine.
Is the default bindingmode changed or is it stricter then before?
Don't get me wrong. I should have added that bindingmode before, but I was just wondering...
Edit
The Property wasn't changed. It had always a private setter. I only did the upgrade, nothing more.
I have just quadruple-checked again with the code for VS2012 and there it runs fine without any exceptions thrown.
You found a security bug in the .net framework which was introduced in 4.5. Private setters should not handle the databinding of a TwoWay binding. In the following issue microsoft commented on the issue:
Connect bug two way databinding on private methods
Security update for two way binding on private methods
Whenever I am doing XAML, I tend to run into problems with databinding. It is often small issues, such as misspelling a property name.
My problem is, that I don't seem to receive any errors or warnings when I am trying to bind to a property that does not exist. It would be nice to get a warning, either at compile or runtime, about my errors.
Is it possible to get a warning about wrong databinding expressions ? What do you do to troubleshoot when your data does not appear as expected ?
WPF will write warnings about problems in data binding to the Visual Studio "Output" window
This blog post, How can I debug WPF bindings? helped me learn why my Binding was failing.
I've accepted the other answer to this question because it is technically correct, but I wanted to include the link above in this question, for future reference.
I want to bind Validation.HasError to a boolean property on my viewmodel. I only want to know when there is a validation error so that I can disable my buttons on my VM using canexecute methods from my relay commands. I get this error though:
'Validation.HasError' property is read-only and cannot be set from markup.
I have tried different modes and they all cause this error. How do I do this? It shouldnt be this difficult.
You can't set a Binding on a read-only dependency property. This is a known bug at Microsoft Connect. You might vote for it.
The BookLibrary sample application of the WPF Application Framework (WAF) shows how to listen to the Validation.HasError property and disable some buttons.
To my understanding, binding errors are displayed only during debug mode in Visual Studio's Output window. However, I want to know about broken bindings when the user runs my app, and I want to notify him that something is not working quite right.
Is there a way to handle binding exceptions from code, when the datacontext is set, and some bindings are broken?
It's possible to catch all Silverlight binding exceptions in all property setters. See here.