Winforms 如何阻止绑定到业务对象的自定义控件进行不必要的更新?

Winforms 如何阻止绑定到业务对象的自定义控件进行不必要的更新?,winforms,data-binding,custom-controls,Winforms,Data Binding,Custom Controls,我有一个Windows窗体应用程序,带有用于输入日期的自定义控件。这是从Textbox继承的,并实现了一个用于绑定到底层业务对象的Value属性 除控件验证时更新绑定属性外,所有操作都运行良好,即使绑定属性没有更改。这是一个问题,因为我使用的是实体框架,对实体属性的更改会导致每次用户打开和关闭承载此控件的表单时,数据库中相应的字段都会更新 代码如下: Public Class TextBoxDate Inherits TextBox Public ValueChanged As EventHa

我有一个Windows窗体应用程序,带有用于输入日期的自定义控件。这是从Textbox继承的,并实现了一个用于绑定到底层业务对象的Value属性

除控件验证时更新绑定属性外,所有操作都运行良好,即使绑定属性没有更改。这是一个问题,因为我使用的是实体框架,对实体属性的更改会导致每次用户打开和关闭承载此控件的表单时,数据库中相应的字段都会更新

代码如下:

Public Class TextBoxDate
Inherits TextBox

Public ValueChanged As EventHandler

Private _dateValue As Nullable(Of Date) = Nothing
<Bindable(True)> _
<Category("Appearance")> _
Public Property Value() As Nullable(Of Date)
    ' Bind to the Value property instead of the Text property, as the latter will not allow
    ' the user to delete the contents of the textbox. The Value property provides support for nulls.
    Get
        Value = _dateValue
    End Get
    Set(ByVal value As Nullable(Of Date))
        _dateValue = value
        ' Update the text in the textbox
        If value.HasValue Then
            Text = CDate(value).ToShortDateString
        Else
            Text = vbNullString
        End If
        OnValueChanged()
    End Set
End Property

Private Sub OnValueChanged()
    If (ValueChanged IsNot Nothing) Then
        ValueChanged(Me, New EventArgs())
    End If

End Sub

Private Sub TextBoxDate_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter

    _userEntered = True

End Sub

Private Sub TextBoxDate_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave

    _userEntered = False

End Sub

Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)

    If _userEntered Then
        If Me.TextLength = 0 Then
            ' Null value will be saved to the database via the bound Value property
            If Value IsNot Nothing Then
                Value = Nothing
            End If
        Else
            Dim dateValue As Date
            If Date.TryParseExact(Text, "d/M/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None, dateValue) Then
                If Value Is Nothing Or (dateValue <> Value) Then
                    Value = CDate(Text)
                End If
            Else
                e.Cancel = True
            End If
        End If
    End If

    MyBase.OnValidating(e)

End Sub

End Class
公共类TextBoxDate
继承文本框
公共价值已更改为事件处理程序
Private _dateValue可为空(日期的)=无
_
_
公共属性值()可为空(自日期起)
'绑定到Value属性而不是Text属性,因为后者不允许
'允许用户删除文本框的内容。Value属性提供对null的支持。
得到
值=\u日期值
结束
设置(ByVal值为空(日期))
_dateValue=值
'更新文本框中的文本
如果value.HasValue那么
Text=CDate(值).toSortDateString
其他的
Text=vbNullString
如果结束
OnValueChanged()
端集
端属性
私有子项OnValueChanged()
如果(ValueChanged不是空的),那么
ValueChanged(Me,新事件参数())
如果结束
端接头
私有子文本框date_Enter(ByVal sender作为对象,ByVal e作为System.EventArgs)处理我。Enter
_userEntered=True
端接头
私有子文本框date_Leave(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Leave
_userEntered=False
端接头
验证时受保护的覆盖子对象(ByVal e作为System.ComponentModel.CancelEventArgs)
如果用户输入了,则
如果Me.TextLength=0,则
'空值将通过绑定值属性保存到数据库
如果值不是零,那么
值=零
如果结束
其他的
将日期值设置为日期
如果Date.TryParseExact(文本“d/M/yyyy”、System.Globalization.DateTimeFormatInfo.InvariantInfo、Globalization.datetimestyle.None、dateValue),则
如果值为Nothing或(dateValue值),则
值=CDate(文本)
如果结束
其他的
e、 取消=真
如果结束
如果结束
如果结束
MyBase.OnValidating(e)
端接头
末级
这让我快发疯了。任何帮助都将不胜感激


Scott

定义文本框的绑定时,可以指定绑定对象的绑定,例如

Binding custNameBinding = new Binding("Text", ds, "customers.custName");
custNameBinding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
custNameTextBox.DataBindings.Add(custNameBinding);
MSDN文档说明“每当控件属性的值发生更改时,数据源都会更新。”