2013-10-14 40 views
0

我有以下XAML這是不工作:如何將數據綁定到視圖模型中定義的屬性的子屬性

... 
<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="OrganisationTemplate"> 
     <Grid Margin="40,0,0,0"> 
      <StackPanel> 
       <TextBlock Text="{Binding name}"></TextBlock> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 
    <DataTemplate x:Key="UserStatusTemplate"> 
     <Grid Margin="40,0,0,0"> 
      <StackPanel> 
       <TextBlock Text="{Binding Path=profile.name}"></TextBlock> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 
</phone:PhoneApplicationPage.Resources>  
    ... 

     <!--Panorama item one--> 
     <phone:PanoramaItem Header="Home"> 
      <ListBox Name="UserStatus" ItemTemplate="{StaticResource UserStatusTemplate}" ItemsSource="{Binding UserStatus}" Margin="0,0,0,96" IsSynchronizedWithCurrentItem="False"/> 
     </phone:PanoramaItem> 

我有以下視圖模型

public class Sections 
{ 
    public IEnumerable<Organisation> Organisations { get; set; } 
    public User User { get; set; } 
    public UserStatus UserStatus { get; set; } 
} 

class DashboardViewModel : INotifyPropertyChanged 
{ 
    public Sections sections = new Sections(); 

    private OrganisationRepository organisationRepository { get; set; } 
    private UserRepository userRepository { get; set; } 

    public DashboardViewModel() 
    { 
     LoadOrganisationSection(); 
     LoadHomeSection(); 
    } 

    ... 

    private async void LoadHomeSection() 
    { 
     userRepository = new UserRepository(); 
     sections.UserStatus = await userRepository.GetStatus(); 
     UserStatus = null; 
    } 

    #region properties 
    public UserStatus UserStatus 
    { 
     get 
     { 
      return sections.UserStatus; 
     } 
     set 
     { 
      if (sections.UserStatus != value) 
      { 
       //LoginCredentials.Username = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 
    ... 
    #endregion 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler tempEvent = PropertyChanged; 

     if (tempEvent != null) 
     { 
      // property changed 
      tempEvent(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

Inside LoadHomeSectionsection.UserStatus有一個屬性填滿了(我調試過了,所以它是de finetly那裏)與路線:section.UserStatus.profile.name,顯然這被反映在暴露於視圖模型屬性:UserStatus.profile.name

如何訪問在子屬性我XAML?

我有工作,如果屬性來訪問是直的還有其他屬性(見OrganisationTemplate

我所看到的其他職位,但我不能讓他們的版本一起使用。

回答

0

啊,那是因爲你不能在列表框使用一個單一的項目... duhhh:

<phone:PanoramaItem Header="Home"> 
       <TextBlock Name="UserStatus" Text="{Binding UserStatus.profile.name}" /> 
       <!--<ListBox Name="UserStatus" ItemTemplate="{StaticResource UserStatusTemplate}" ItemsSource="{Binding UserStatus}" Margin="0,0,0,96" IsSynchronizedWithCurrentItem="False"/>--> 
      </phone:PanoramaItem> 
相關問題