我制作了一个带有复选框的小型 UserControl,并尝试将其 DataBind 到类的布尔属性。Binding的DataSourceUpdateMode设置为OnPropertyChanged。不幸的是,它没有按预期工作。
如果我更改我的类属性,复选框会立即做出反应,但不会反过来。单击复选框不会立即更改类属性。只有当您离开用户控件(pe 进入另一个控件)时,类属性才会更新。我猜是因为复选框只有在离开后才会被验证。但为什么?
任何想法都非常感谢!
问候
基督教
这是一些简单的代码(不是 c&p,调试器不会在真实代码上给我任何错误):
班上:
Imports System.ComponentModel
Public Class _testClass
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Private _Status As Boolean
Public Property Status() As Boolean
Get
Return _Status
End Get
Set(ByVal value As Boolean)
_Status = value
NotifyPropertyChanged("Status")
Debug.WriteLine("CLASS " & value)
End Set
End Property
End Class
用户控件:
Imports System.ComponentModel
Public Class _testcontrol
Public Event Valuechanged As EventHandler
Public Property OnOff() As Boolean
Get
Return CheckBox1.Checked
End Get
Set(ByVal value As Boolean)
CheckBox1.Checked = value
Debug.WriteLine("PROPERTY " & value)
End Set
End Property
Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
RaiseEvent Valuechanged(sender, e)
Debug.WriteLine("EVENT")
OnOff = CheckBox1.Checked
End Sub
End Class
数据绑定:
Public Class Form1
Dim testclass As New _testClass
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Testcontrol.DataBindings.Add(New Binding("OnOff", Me.testclass, "Status", True, DataSourceUpdateMode.OnPropertyChanged))
End Sub
End Class