2014-04-27 69 views
1

我想要綁定包含ObservableCollection屬性的集合(「儀表板」)的數據。在XAML中的堆棧面板中的DataContext Gridview 8 Windows Phone 8

我設法數據綁定儀表板類沒有任何問題。然而,我不知道如何將數據綁定到儀表板類中包含的Release集合。

這個問題似乎在GridView上,它是Dashboard類的Releases屬性的數據綁定。 GridView周圍的堆棧面板工作正常。

的類

public class Dashboard 
{ 
    public Dashboard(String id, String projectName) 
    { 
     this.Id = id; 
     this.ProjectName = projectName; 
     this.Releases = new ObservableCollection<Release>(); 
    } 

    public string Id { get; private set; } 
    public string ProjectName { get; private set; } 

    public ObservableCollection<Release> Releases { get; private set; } 

    public override string ToString() 
    { 
     return this.ProjectName; 
    } 
} 

public class Release 
{ 
    public Release(string environmentName, string releaseVersion, string state, string releaseDate) 
    { 
     EnvironmentName = environmentName; 
     ReleaseVersion = releaseVersion; 
     State = state; 
     ReleaseDate = releaseDate; 
    } 

    public string EnvironmentName { get; private set; } 
    public string ReleaseVersion { get; private set; } 
    public string State { get; private set; } 
    public string ReleaseDate { get; private set; } 
} 

的XAML

<HubSection x:Uid="Dashboard" x:Name="Dashboard" Header="Dashboard" DataContext="{Binding Dashboard}"> 
      <DataTemplate> 
       <GridView x:Uid="DashboardGrid" x:Name="DashboardGrid" ItemsSource="{Binding}" ItemTemplate="{StaticResource Standard200x180TileItemTemplate}" > 
        <GridView.ItemsPanel> 
         <ItemsPanelTemplate> 
          <ItemsWrapGrid /> 
         </ItemsPanelTemplate> 
        </GridView.ItemsPanel> 
       </GridView> 
      </DataTemplate> 
     </HubSection> 

數據模板

<DataTemplate x:Key="Standard200x180TileItemTemplate"> 
     <StackPanel DataContext="{Binding}" > 

      <TextBlock Text="{Binding ProjectName}" Grid.Column="0" Style="{ThemeResource BaseTextBlockStyle}" Typography.Capitals="SmallCaps" Grid.Row="0" IsTextScaleFactorEnabled="False"/> 

      <GridView Grid.Row="1" DataContext="{Binding Releases}"> 

       <TextBlock Text="{Binding EnvironmentName}" /> 
       <TextBlock Text="{Binding ReleaseVersion}" /> 
       <TextBlock>hello</TextBlock> 

       <Border Background="#FF0CB90C" Height="110" Width="110" HorizontalAlignment="Left" Margin="0,0,10,0"> 

       </Border> 
      </GridView> 

     </StackPanel> 
    </DataTemplate> 

回答

1

集的GridView的的ItemsSource不是DataContext的,然後用另一DataTemplate中

<StackPanel> 
     <TextBlock Text="{Binding ProjectName}" Grid.Column="0" Style="{ThemeResource BaseTextBlockStyle}" Typography.Capitals="SmallCaps" Grid.Row="0" IsTextScaleFactorEnabled="False"/> 
     <GridView Grid.Row="1" ItemsSource="{Binding Releases}"> 
      <GridView.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding EnvironmentName}" /> 
       <TextBlock Text="{Binding ReleaseVersion}" /> 
       <TextBlock>hello</TextBlock> 
       <Border Background="#FF0CB90C" Height="110" Width="110" HorizontalAlignment="Left" Margin="0,0,10,0"> 
       </Border> 
      </DataTemplate> 
      </GridView.ItemTemplate> 
     </GridView> 
    </StackPanel> 
+0

完美謝謝 – Komainu85