TextBox1绑定的问题

im战术 2017-05-14 05:46:20

Public Class Form1
Class BindingData
Property A As String
End Class

Dim BindingDataNew As New BindingData
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindingDataNew.A= 10
TextBox1.DataBindings.Add("Text", BindingDataNew, "A", False, DataSourceUpdateMode.OnPropertyChanged)
'BindingDataNew.BindingData = 10 '加在下面运行时不能同步显示
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BindingDataNew.A = 999
MsgBox(BindingDataNew.A & "-----------" & TextBox1.Text)
'结果为 999-----------空
End Sub
End Class

手动输入文本到TextBox1里绑定数据是同步的
但给数据赋值时并不同步显示出来和赋值到TextBox1.Text里
所以DataBindings是单向绑定吗!
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
im战术 2017-05-15
  • 打赏
  • 举报
回复
HOHO成功了,我看晕了我试验时把其中一个

'NotifyPropertyChanged("MyText")
'它不小心被我注解了,使用它就OK了.
im战术 2017-05-15
  • 打赏
  • 举报
回复

    Private Sub MyTB_TextChanged(sender As Object, e As EventArgs) Handles MyTB.TextChanged
        '========Sorry少加了这句========
        MyTB.MyText = MyTB.Text
        '========Sorry少加了这句========
        Text = $"数据为: {数据.Data} MyText为: {MyTB.MyText}"
    End Sub
但还是一样MyText改变不能同步Data
im战术 2017-05-15
  • 打赏
  • 举报
回复

Imports System.ComponentModel

Public Class Form1
    Dim 数据 As New CData
    WithEvents MyTB As New MyTextBox
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MyTB.Location = New Point(100, 100)
        Controls.Add(MyTB)
        MyTB.DataBindings.Add(New Binding("MyText", 数据, "Data", False, DataSourceUpdateMode.OnPropertyChanged))
    End Sub

    Private Sub MyTB_TextChanged(sender As Object, e As EventArgs) Handles MyTB.TextChanged
        Text = $"数据为: {数据.Data} MyText为: {MyTB.MyText}"
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        数据.Data = TextBox2.Text
        Text = $"数据为: {数据.Data} MyText为: {MyTB.MyText}"
    End Sub
End Class

Public Class MyTextBox
    Inherits TextBox
    Implements INotifyPropertyChanged

    Private _MyText As String
    Public Property MyText() As String
        Get
            Return _MyText
        End Get
        Set(ByVal value As String)
            _MyText = value
            NotifyPropertyChanged("MyText")
        End Set
    End Property


    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

Public Class CData
    Implements INotifyPropertyChanged

    Private _Data As String
    Public Property Data() As String
        Get
            Return _Data
        End Get
        Set(ByVal value As String)
            _Data = value
            NotifyPropertyChanged("Data")
        End Set
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Sub NotifyPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class
那为什么自定义添加的属性就不能同步呢! 是我哪里搞错了吗? 改数据能同步文本 改文本这样的代码就不能同步数据!
xdashewan 2017-05-15
  • 打赏
  • 举报
回复
双向时,你需要继承INotifyPropertyChanged接口,参照https://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged(VS.95).aspx banding时使用

textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", 对象, "FirstName", false, DataSourceUpdateMode.OnPropertyChanged));

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧