Ok, so I have been trying to iron out this data binding for a while now and every time I think I get it, it doesn't work. Please tell me where I am going wrong. Let me know if you need more info, but basically, for right now, I have this as my wAddClient.xaml.vb:
Imports System.ComponentModel
Public Class WAddClient
Implements INotifyPropertyChanged
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
DataContext = newClient
newClient = New Client()
End Sub
Private _newClient As Client
Public Property newClient As Client
Get
Return _newClient
End Get
Set(ByVal value As Client)
_newClient = value
NotifyPropertyChanged("newClient")
End Set
End Property
Public Event PropertyChanged(开发者_运维百科ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
End Class
Then this is what I am trying to do as far as the binding, this is the XAML:
<TextBox Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="4" Name="txtFirstName" Margin="0,0,5,0">
<TextBox.Text>
<Binding Source="Me" Path="newClient.first_name" Mode="TwoWay"
ValidatesOnDataErrors="True" ValidatesOnExceptions="True"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
So as you can guess, client has a property first_name that I want to bind. Here is partial code for the client class.
Imports System.ComponentModel
Public Class Client
Implements INotifyPropertyChanged
Implements IDataErrorInfo
'Constructors
Public Sub New()
salutation = "Mr."
first_name = "Kevin"
last_name = "Tester"
email = ""
primary_phone = ""
address_1 = ""
city = ""
state_code = ""
zip_code = ""
address_2 = ""
secondary_phone = ""
fax = ""
End Sub
So I do not understand what I am doing wrong. Please help.
In your binding: Source="Me"
that is wrong.
Remove that and assign an instance of a Client to the DataContext. The Binding's Path will be relative to that object.
Also make sure the Client class uses public properties that will be bound to. Fields do not support binding.
Sorry my VB.net skills are non existent. But lets try it anyway:
DataContext = newClient
newClient = New Client()
I think you should switch these two lines. Not sure how referencing in VB.net works tho.
Also you didn't mentioned whats not working exactly. So i would suggest you download Snoop, to see if the DataContext is correct.
精彩评论