Winform-Custom属性中的DataBinding不起作用

时间:2013-09-30 07:42:00

标签: c# winforms data-binding

对于兼容的Null i覆盖的Value属性。但是此属性不能在DataBindings中使用。当控件的值更新时,它不会改变。我将控件更改为DateTimePicker,一切都很好.Value属性出了什么问题?

测试类

class prod
{
    int id;

    public int Id {
        get { return id; }
        set { id = value; }
    }

    DateTime? md;
    public Nullable<DateTime> Md {
        get { return md; }
        set { md = value; }
    }
}

//自定义DateTimePicker

    [Bindable(true), Browsable(true)]
    public new object Value
    {
        get
        {
            if (realDate)
            {
                return base.Value;
            }
            else
            {
                return DBNull.Value; //If not a real date, sent DBNull to the bound field
            }
        }
        set
        {
            if (Convert.IsDBNull(value))
            {
                realDate = false;
                oldFormat = Format; //Store the Format of the datetimepicker
                Format = DateTimePickerFormat.Custom;
                CustomFormat = " "; //With this custom format, the datetimepicker is empty
            }
            else
            {
                realDate = true;
                CustomFormat = null;
                base.Value = Convert.ToDateTime(value);
            }
            OnValueChanged();
        }
    }

//绑定代码:

    prod pp=new prod();
    datePicker1.DataBindings.Add("Value",pp,"Md",true,DataSourceUpdateMode.OnValidation);

我发现问题----需要在selectitemchanged中为每个数据绑定使用WriteValue。

1 个答案:

答案 0 :(得分:0)

使用 DataSourceUpdateMode.OnPropertyChanged 代替 DataSourceUpdateMode.OnValidation