VB.NET WPF TextBox与类对象绑定
REM TextBox类 :TextBoxCdrObjWidth 和 TextBoxCdrObjHeight
Dim CdrObjSize As New CdrObjectSizeClass() ' 要绑定的数据类
Try
Dim BindingWidth As New Binding
Dim BindingHeight As New Binding
BindingWidth.Source = CdrObjSize
BindingWidth.Mode = BindingMode.TwoWay
'BindingMode.TwoWay双向绑定,即修改TextBox.Text属性,源目标变量发生改变,更源目标变量改变时PropertyChaned,TextBox.Text属性也发生改变
BindingWidth.Path = New PropertyPath("WidthSize")
BindingWidth.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
TextBoxCdrObjWidth.SetBinding(TextBox.TextProperty, BindingWidth)
'代码透透气---------分隔线-----------------------------------------------------------------------------
BindingHeight.Source = CdrObjSize
BindingHeight.Mode = BindingMode.TwoWay '双向绑定
BindingHeight.Path = New PropertyPath("HeightSize")
BindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
TextBoxCdrObjHeight.SetBinding(TextBox.TextProperty, BindingHeight)
Catch ex As Exception
MsgBox(ex.Message)
End Try
REM ----------------------------------------------------------------------------------------------------------------------------------------
‘添加属性更改接口,实现双向数据绑定:
在Public Class CdrObjectSizeClass类中添加
Imports System.ComponentModel
Public Class CdrObjectSizeClass
Implements INotifyPropertyChanged '提供接口,属性更改通知
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged '声明事件
'使用Get 和 Set传值与主窗口TextBox数据绑定
Public Property HeightSize As String
Get
Return CStr(Height)
End Get
Set(ByVal value As String)
If value <> Height Then
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("HeightSize"))
End If
Height = CDbl(value)
End Set
End Property
REM注意,用双引号即触发HeightSize对象本身(真正Me.PropertyChanged属性的更改,不带双引号即是传递HeightSize的Double值,并没有真正把事件传给TextBox绑定数据更新:即BindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
Dim CdrObjSize As New CdrObjectSizeClass() ' 要绑定的数据类
Try
Dim BindingWidth As New Binding
Dim BindingHeight As New Binding
BindingWidth.Source = CdrObjSize
BindingWidth.Mode = BindingMode.TwoWay
'BindingMode.TwoWay双向绑定,即修改TextBox.Text属性,源目标变量发生改变,更源目标变量改变时PropertyChaned,TextBox.Text属性也发生改变
BindingWidth.Path = New PropertyPath("WidthSize")
BindingWidth.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
TextBoxCdrObjWidth.SetBinding(TextBox.TextProperty, BindingWidth)
'代码透透气---------分隔线-----------------------------------------------------------------------------
BindingHeight.Source = CdrObjSize
BindingHeight.Mode = BindingMode.TwoWay '双向绑定
BindingHeight.Path = New PropertyPath("HeightSize")
BindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
TextBoxCdrObjHeight.SetBinding(TextBox.TextProperty, BindingHeight)
Catch ex As Exception
MsgBox(ex.Message)
End Try
REM ----------------------------------------------------------------------------------------------------------------------------------------
‘添加属性更改接口,实现双向数据绑定:
在Public Class CdrObjectSizeClass类中添加
Imports System.ComponentModel
Public Class CdrObjectSizeClass
Implements INotifyPropertyChanged '提供接口,属性更改通知
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged '声明事件
'使用Get 和 Set传值与主窗口TextBox数据绑定
Public Property HeightSize As String
Get
Return CStr(Height)
End Get
Set(ByVal value As String)
If value <> Height Then
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("HeightSize"))
End If
Height = CDbl(value)
End Set
End Property
REM注意,用双引号即触发HeightSize对象本身(真正Me.PropertyChanged属性的更改,不带双引号即是传递HeightSize的Double值,并没有真正把事件传给TextBox绑定数据更新:即BindingHeight.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.