数据绑定帮助WPF VB.NET

时间:2011-07-04 13:28:57

标签: wpf vb.net xaml data-binding

好的,所以我一直试图解决这个数据绑定一段时间,每次我想我得到它,它都行不通。请告诉我哪里出错了。如果您需要更多信息,请告诉我,但基本上,就目前而言,我将此作为我的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

然后就绑定而言,这就是我要做的,这就是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>

您可以猜到,客户端有一个我要绑定的属性first_name。这是客户端类的部分代码。

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

所以我不明白我做错了什么。请帮忙。

2 个答案:

答案 0 :(得分:2)

抱歉,我的VB.net技能不存在。但是让我们尝试一下:

    DataContext = newClient
    newClient = New Client()

我认为你应该切换这两行。不确定VB.net中的引用是如何工作的。

你也没有提到什么不完全正常。所以我建议你下载Snoop,看看DataContext是否正确。

答案 1 :(得分:2)

在你的绑定中:Source="Me"这是错误的。

删除它并将客户端的实例分配给DataContext。 Binding的路径将与该对象相关。

还要确保Client类使用将绑定的公共属性。字段不支持绑定。