Consider this source:
public partial class FormTest : Form
{
private Test test { get; set; }
public FormTest()
{
this.InitializeComponent();
this.test = new Test();
this.text_box.DataBindings.Add(new CustomBinding("Text", this.test, "some_int", false, DataSourceUpdateMode.OnPropertyChanged));
}
}
class CustomBinding : Binding
{
protected override void OnBindingComplete(BindingCompleteEventArgs e)
{
base.OnBindingComplete(e);
MessageBox.Show("Yes!");
}
protected override void OnParse(ConvertEventArgs cevent)
{
Type new_type = cevent.DesiredType;
Object new_value = cevent.Value;
if (new_type.IsGenericType && new_type.GetGenericTypeDefinition() == typeof(Nullable<>))
if (!new_value.GetType().IsValueType)
new_type = new_type.GetGenericArguments()[0];
else
new_value = new_type.GetConstructor(new Type[] { new_value.GetType() }).Invoke(new Object[] { new_value });
base.OnParse(new ConvertEventArgs(new_value, new_type));
}
public CustomBinding(String property_name, Object data_source, String data_member, Boolean formatting, DataSourceUpdateMode update_mode)
: base(property_name, data_source, data_member, formatting, update_mode) { }
}
class Test : INotifyPropertyChanged
{
private Int32? _some_int;
public Int32? some_int
{
get
{
return this._some_int;
}
set
{
this._some_int = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("some_int"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
when data is inputted into the tex_box a message is showed in console: "A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll" and the binding complete is never reached. The code inside th OnParse is tested and working, but i cannot resolve this issue...
please help.