数据绑定到主Dataobject的各个属性

时间:2010-05-26 13:36:11

标签: wpf data-binding mvvm

这是我的ViewModel代码: VB:

Public Property Doctor() As Doctor
        Get
            Return _objDoctor
        End Get
        Set(ByVal Value As Doctor)
            _objDoctor = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property AddDate() As Nullable(Of DateTime)
        Get
            Return _objDoctor.AddDate
        End Get
        Set(ByVal Value As Nullable(Of DateTime))
            _objDoctor.AddDate = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property AddUserID() As String
        Get
            Return _objDoctor.AddUserID
        End Get
        Set(ByVal Value As String)
            _objDoctor.AddUserID = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property ChangeDate() As Nullable(Of DateTime)
        Get
            Return _objDoctor.ChangeDate
        End Get
        Set(ByVal Value As Nullable(Of DateTime))
            _objDoctor.ChangeDate = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property ChangeUserID() As String
        Get
            Return _objDoctor.ChangeUserID
        End Get
        Set(ByVal Value As String)
            _objDoctor.ChangeUserID = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property CollaboratingPhysicianID() As Nullable(Of Int64)
        Get
            Return _objDoctor.CollaboratingPhysicianID
        End Get
        Set(ByVal Value As Nullable(Of Int64))
            _objDoctor.CollaboratingPhysicianID = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property CredentialsID() As Int64
        Get
            Return _objDoctor.CredentialsID
        End Get
        Set(ByVal Value As Int64)
            _objDoctor.CredentialsID = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property DisplayName() As String
        Get
            Return _objDoctor.DisplayName
        End Get
        Set(ByVal Value As String)
            _objDoctor.DisplayName = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property DoctorSpecialtyID() As Int64
        Get
            Return _objDoctor.DoctorSpecialtyID
        End Get
        Set(ByVal Value As Int64)
            _objDoctor.DoctorSpecialtyID = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property FirstName() As String
        Get
            Return _objDoctor.FirstName
        End Get
        Set(ByVal Value As String)
            _objDoctor.FirstName = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property ID() As Int64
        Get
            Return _objDoctor.ID
        End Get
        Set(ByVal Value As Int64)
            _objDoctor.ID = Value
            OnPropertyChanged("Doctor")
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return _objDoctor.LastName
        End Get
        Set(ByVal Value As String)
            _objDoctor.LastName = Value
            OnPropertyChanged("LastName")
        End Set
    End Property

以下是我可以使用的XAML示例:

<StackPanel>
        <TextBox Text="{Binding LastName}" />
        <TextBox Text="{Binding Doctor.LastName}" />
    </StackPanel>

我真的更喜欢绑定到我的数据,因为第一个文本框是我可以对更改进行验证。 (如果有办法用第二种方式进行验证,有人请告诉我)

问题在于:

如果我在viewmodel的新构造函数中加载医生:

Public Sub New()
    If IsInDesignMode Then
    Else
        CreateEventSubscriptions()
    End If
    Me.Doctor = DoctorService.GetDoctor(4839)

End Sub

Databinding第一次正常工作。但是,如果我尝试使用事件更改关联的医生,则只有doctor.lastname绑定有效。

    Private Sub onEdit(ByVal d As Doctor)
        Me.Doctor = DoctorService.GetDoctor(d)


    End Sub

我知道我不需要从我的服务中加载医生,因为我实际上正在通过医生......我试图看看是否有一些关于使用服务来填补这个问题的神奇之处。

我使用Karl Shifflet的XAML PowerToys获取了我的viewmodel属性的代码。知道卡尔知道他在做什么我不知道为什么我不能让它工作......希望这是我想念的傻事。

1 个答案:

答案 0 :(得分:1)

我相信如果你在活动期间换了医生,你也应该打电话:

OnPropertyChanged("LastName")

否则我认为绑定代码无法知道LastName属性是否已更新。