views:

346

answers:

1

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.

+1  A: 

tried to play around with this, and here's how I solved it - when creating the binding on the text box:

this.text_box.DataBindings.Add(new CustomBinding("Text", this.test, "some_int", false, DataSourceUpdateMode.OnPropertyChanged));

set the formatting flag to "true" (it's the third parameter).

once you do that, you don't need the custom binding anymore...I commented the code in OnParse out and it works. I use just a Binding object instead of CustomBinding, and it still works :)

check this blog for some details: http://blog.jezhumble.net/?p=3

Ami
its appear thats the very only way to Bind to a nullable, i already knew that we put formatting to true would work, however i have created the CustomBinding just to do my own casting from and to a nulable, but i cant find a way to work. thanks for your attention and for the anser that correctly solve my problem.
Leonardo Justino