Private Sub DataBindingsToControls(con As Control, src As Object, srcProperty As String) Dim prop As String = "" Select Case True Case TypeOf con Is TextBoxBase
' 双方向にするため、BindingComplete イベント内で ReadValue メソッドを呼び出す AddHandler con.DataBindings.Add("Text", src, srcProperty, True, DataSourceUpdateMode.OnValidation).BindingComplete, _ Sub(sender As Object, e As BindingCompleteEventArgs) e.Binding.ReadValue() End Sub
Case TypeOf con Is Label con.DataBindings.Add("Text", src, srcProperty, True, DataSourceUpdateMode.OnValidation) Case TypeOf con Is ComboBox DataBindings.Add("SelectedValue", src, srcProperty, True, DataSourceUpdateMode.OnPropertyChanged) Case TypeOf con Is CheckBox, TypeOf con Is RadioButton DataBindings.Add("Checked", src, srcProperty, True, DataSourceUpdateMode.Never) Case TypeOf con Is DateTimePicker DataBindings.Add("Value", src, srcProperty, True, DataSourceUpdateMode.OnValidation) End Select End Sub
#Region "Common.FindControl の仕様が分からないけど、こんな感じだろうか" Private Function FindControl(container As Control, name As String) As Control Return FindControls(container).FirstOrDefault(Function(c) c.Name = name) End Function Private Function FindControls(container As Control) As IEnumerable(Of Control) Dim controls = container.Controls.Cast(Of Control)() Dim children = controls.Select(AddressOf FindControls) Return controls.Concat(children.SelectMany(Function(c) c)) End Function #End Region
お世話になっております。
DataBindingsで自作クラスのプロパティとフォームのコントロールを紐づけ、
PropertyChangedでプロパティの変更を拾い、処理を行おうとしています。
customer_code テキストボックス の値の変更を拾い、customer_name テキストボックスに
値をセットすることはできるのですが、桁を補完した customer_code がテキストボックスに反映されません。
原因と対処法をお教えください。
よろしくお願いいたします。
以下、ソース(一部抜粋)
Public Class ModelBase
Implements INotifyPropertyChanged
Protected Overridable Sub OnPropertyChanged(<CallerMemberName()> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class
Public Class Customers
Inherits ModelBase
Private _customer_code As String
Public Property customer_code As String
Get
Return _customer_code
End Get
Set(value As String)
If Not value = _customer_code Then
_customer_code = value
OnPropertyChanged()
End If
End Set
End Property
Public Property customer_name As String
End Class
Public Class Form1
Private WithEvents head As Customers
Sub New()
InitializeComponent()
head = New Customers
AddHandler head.PropertyChanged, AddressOf HeaderProperties_ValueChanged
SetupHeaderControls()
End Sub
Private Sub SetupHeaderControls()
For Each prop In head.GetType.GetProperties
Select Case prop.Name
Case "is_output", "created_at", "created_name", "updated_at", "updated_name", "order_date"
'何もしない
Case Else
'同名のコントロールを取得
Dim con As Control = common.FindControl(Me, prop.Name)
'プロパティとコントロールの紐づけ
DataBindingsToControls(con, head, prop.Name)
End Select
Next
End Sub
Private Sub DataBindingsToControls(con As Control, src As Object, srcProperty As String)
Dim prop As String = ""
Select Case True
Case TypeOf con Is TextBox, TypeOf con Is Label
con.DataBindings.Add("Text", src, srcProperty, True, DataSourceUpdateMode.OnValidation)
Case TypeOf con Is ComboBox
con.DataBindings.Add("SelectedValue", src, srcProperty, True, DataSourceUpdateMode.OnPropertyChanged)
Case TypeOf con Is CheckBox, TypeOf con Is RadioButton
con.DataBindings.Add("Checked", src, srcProperty, True, DataSourceUpdateMode.Never)
Case TypeOf con Is DateTimePicker
con.DataBindings.Add("Value", src, srcProperty, True, DataSourceUpdateMode.OnValidation)
End Select
End Sub
Private Sub HeaderProperties_ValueChanged(sender As Object, e As PropertyChangedEventArgs) Handles head.PropertyChanged
If e.PropertyName = "customer_code" Then
If Not head.customer_code = "" Then
Dim masterUtil As New MasterUtil
'入力値の前を"0"で埋める
head.customer_code = common.SetPrefix(head.customer_code, 6, "0") '←プロパティの値は桁補完できているが、テキストボックスに反映されない
head.customer_name = masterUtil.GetCustomerName(head.customer_code)
End If
End If
End Sub
End Class