I have a bunch of Checkboxes on my form with their Checked properties bound to Boolean properties on the data model:
chk1.DataBindings.Add(new BindingValue(this, "Checked", "MyBooleanProperty1", false))
chk2.DataBindings.Add(new BindingValue(this, "Checked", "MyBooleanProperty2", false))
chk3.DataBindings.Add(new BindingValue(this, "Checked", "MyBooleanProperty3", false))
There is also a shared event handler for all checkboxes on the screen that ensures the databound value is correctly set to the checked value.
private void AllCheckboxes_CheckedChanged(object sender, EventArgs e)
{
var chk = ((CheckBox)sender);
var binding = chk.DataBindings["Checked"];
if (binding != null)
binding.WriteValue();
}
In some cases, the first time this form and the bindings are loaded, I get an exception :
Cannot format the value to the desired type.
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value) at System.Windows.Forms.BindToObject.SetValue(Object value) at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force) at System.Windows.Forms.Binding.WriteValue()
It works correctly for the first checkbox to process the event, but then the second one will throw this exception.
The data source is an interface of my datamodel
public interface IMyDataModel
{
bool MyBooleanProperty1 { get; set; }
bool MyBooleanProperty2 { get; set; }
bool MyBooleanProperty3 { get; set; }
}
And I can verify that the data model itself is set correctly by setting a breakpoint right before the .WriteValue in the event handler. I can even put a breakpoint in the setter of the bound boolean property, and it is also called correctly.
If I set the FormattingEnabled
property of the binding to true, it does fix the issue. But I am wondering why I even have to do that in the first place, since I am binding a System.Boolean
property in the UI object to a bool
property on the data source.
Why would I be getting this exception in this case?
It's difficult to say for sure what's going on without a way to reproduce the issue.
But at least I can explain why setting FormattingEnabled
makes the exception go away:
http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Binding.cs,853
WriteData
calls PullData
with reformat = true
and force = true
.
From your call stack, it looks like you must be making it into this block:
// Put the value into the data model
if (!parseFailed) {
this.bindToObject.SetValue(parsedValue);
}
Immediately following that, the exception is rethrown unless FormattingEnabled = true
. So setting FormattingEnabled
here hides the problem, since it seems Binding
assumes that you're going to handle any formatting/parsing issues it's found so far yourself.
As for why the exception is thrown in the first place... I don't know. Nothing is obvious looking here:
http://referencesource.microsoft.com/#system/compmod/system/componentmodel/ReflectPropertyDescriptor.cs,1138
I'd be curious to know if your problem persists if you find a way to add your data bindings without your BindingValue
custom class. I'd also be curious if you have any listeners wired up in BindingValue
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With