2010-05-26 88 views
0

的個別屬性這是我的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> 

我真的喜歡綁定到我的數據作爲第一個文本框,以便我可以對更改進行驗證。 (如果有一種方法可以做到的驗證與第二種方式有人請讓我知道)

問題是這樣的:

如果我在視圖模型的新的構造函數加載醫生這樣:

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

End Sub 

數據綁定第一次正常工作。但是,如果我嘗試用事件更改相關醫生,則只有doctor.lastname綁定有效。

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


    End Sub 

我知道我不需要從我的服務加載一個醫生,因爲我其實經過醫生......我想看看是否有什麼神奇的使用服務來填充屬性格式。

我使用Karl Shifflet的XAML PowerToys獲得了我的viewmodel屬性的代碼。知道卡爾知道他在做什麼,我不知道爲什麼我不能得到這個工作....希望這是我很想念的東西。

回答

1

我相信,如果你在活動期間更換醫生,你也應該叫:

OnPropertyChanged("LastName") 

否則,我不認爲綁定代碼已經知道姓氏屬性已更新的任何方式。

+0

所以我需要把它放在醫生的財產?所以每個屬性需要一個屬性在醫生下更改?生病嘗試..謝謝 – ecathell 2010-05-26 14:00:15

+0

這是神奇的..感謝...我從來沒有想過自己會這樣做......謝謝菲爾 – ecathell 2010-05-26 14:01:25