1

我创建了一个简单的 VB .NET 应用程序。1 个带有 2 个文本框的表单,绑定到具有 2 个属性的类,Property1 和 Property2。如果我在 TextBox1 中输入文本,绑定过程会导致读取第二个属性 Property2。为什么?我不希望如果我更改一个属性,所有有界控件都会更新它们的数据。

代码:

' Class1.vb

Imports System.ComponentModel

Public Class Class1
  Private property1Value As String
  Public Property Property1() As String
    Get
      Return property1Value
    End Get
    Set(ByVal value As String)
      property1Value = value
    End Set
  End Property

  Private property2Value As String
  Public Property Property2() As String
    Get
      Return property2Value
    End Get
    Set(ByVal value As String)
      property2Value = value
    End Set
  End Property

End Class 

' Form1.Designer.vb

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
  Inherits System.Windows.Forms.Form

  'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti.
  <System.Diagnostics.DebuggerNonUserCode()> _
  Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
      If disposing AndAlso components IsNot Nothing Then
        components.Dispose()
      End If
    Finally
      MyBase.Dispose(disposing)
    End Try
  End Sub

  'Richiesto da Progettazione Windows Form
  Private components As System.ComponentModel.IContainer

  'NOTA: la procedura che segue è richiesta da Progettazione Windows Form
  'Può essere modificata in Progettazione Windows Form.  
  'Non modificarla nell'editor del codice.
  <System.Diagnostics.DebuggerStepThrough()> _
  Private Sub InitializeComponent()
    Me.components = New System.ComponentModel.Container()
    Me.TextBox1 = New System.Windows.Forms.TextBox()
    Me.TextBox2 = New System.Windows.Forms.TextBox()
    Me.Class1BindingSource = New System.Windows.Forms.BindingSource(Me.components)
    CType(Me.Class1BindingSource, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'TextBox1
    '
    Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.Class1BindingSource, "Property1", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
    Me.TextBox1.Location = New System.Drawing.Point(12, 12)
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(100, 22)
    Me.TextBox1.TabIndex = 2
    '
    'TextBox2
    '
    Me.TextBox2.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.Class1BindingSource, "Property2", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
    Me.TextBox2.Location = New System.Drawing.Point(12, 40)
    Me.TextBox2.Name = "TextBox2"
    Me.TextBox2.Size = New System.Drawing.Size(100, 22)
    Me.TextBox2.TabIndex = 3
    '
    'Class1BindingSource
    '
    Me.Class1BindingSource.DataSource = GetType(WindowsApplication1.Class1)
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(282, 255)
    Me.Controls.Add(Me.TextBox2)
    Me.Controls.Add(Me.TextBox1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    CType(Me.Class1BindingSource, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    Me.PerformLayout()

  End Sub
  Friend WithEvents Class1BindingSource As System.Windows.Forms.BindingSource
  Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
  Friend WithEvents TextBox2 As System.Windows.Forms.TextBox

End Class

' Form1.vb

Imports System.ComponentModel

Public Class Form1

  Private Class1 As New Class1

  Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Class1.Property1 = "Prop1"
    Class1.Property2 = "Prop2"
    Class1BindingSource.DataSource = Class1
  End Sub
End Class
4

0 回答 0