I defined a textbox and a list, the list has two data, every data has three properties named "Name", "Age" and "Sex". I want the bindind the "Name" of the second data of the list to the textproperty of the textbox, so I write some code like following :
VB code:
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Class Window1
Public MyPerson As New ObservableCollection(Of Person)
Sub New()
InitializeComponent()
MyPerson.Add(New Person("Jackson", 32, "Boy"))
MyPerson.Add(New Person("Mike", 21, "Girl"))
Dim MyBinding As New Binding("MyPerson[1]/Name")
MyBinding.Source = MyPerson
'MyBinding.Path = MyPerson
MyBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
txb.SetBinding(TextBox.TextProperty, MyBinding)
End Sub
End Class
Public Class Person
Implements INotifyPropertyChanged
Private _Name As String
Private _Age As Integer
Private _Sex As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
Me.OnPropertyChanged("Name")
End Set
End Property
Public Property Age() As Integer
Get
Return _Age
End Get
Set(ByVal value As Integer)
_Age = value
Me.OnPropertyChanged("Age")
End Set
End Property
Sub New(ByVal _Name As String, ByVal _Age As Integer, ByVal _Sex As String)
Me._Name = _Name
Me._Age = _Age
Me._Sex = _Sex
End Sub
Public Sub OnPropertyChanged(ByVal Info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Info))
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
Xaml Code:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Name="txb"></TextBox>
</Grid>
</Window>
So the question is that if I want to binding the "Name" property of data ("Mike", 21, "Girl") to the textpropery of the textbox, how should I change the code " Dim MyBinding As New Binding("MyPerson[1]/Name")"? Shold anybody give me some advice? Thank you !