要在代码中绑定独立编辑器,需要把一个 System.Windows.Forms.Binding 对象添加到控件的 Control.DataBindings 集合中。 System.Windows.Forms.Binding.FormattingEnabled 属性必须被设置为 true。
下面的示例展示了如何把 DateEdit 控件绑定到 Entity 业务对象的一个可空类型的属性。
C# | 复制代码 |
---|---|
// An Entity object. Entity _entity = new Entity(); // Bind the editor to the Entity.NullableDateTime property. // Set the Binding.FormattingEnabled property to true. dateEdit1.DataBindings.Add("EditValue", _entity, "NullableDateTime", true); public class Entity { public Entity() { } private DateTime? _nullableDateTime = new DateTime(2001, 1, 1); public DateTime? NullableDateTime { get { return _nullableDateTime; } set { _nullableDateTime = value; } } } |
Visual Basic | 复制代码 |
---|---|
' An Entity object. Dim _entity As Entity = New Entity ' Bind the editor to the Entity.NullableDateTime property. ' Set the Binding.FormattingEnabled property to true. Me.DateEdit1.DataBindings.Add(New System.Windows.Forms.Binding( _ "EditValue", _entity, "NullableDateTime", True)) Public Class Entity Public Sub New() End Sub Private _nullableDateTime As Nullable(Of DateTime) = New DateTime(2001, 1, 1) Public Property NullableDateTime() As Nullable(Of DateTime) Get Return _nullableDateTime End Get Set(ByVal value As Nullable(Of DateTime)) _nullableDateTime = value End Set End Property End Class |