2013-09-10 127 views
4

我试图将一些数据绑定到Windows 8.1的Hub控件中的GridViewGridView中的数据绑定

目前,我有一个DataTemplatePage.Resources下设置如下:

 <DataTemplate x:Key="Standard240x320ItemTemplateFAV"> 
     <Grid HorizontalAlignment="Left" Width="320" Height="240"> 
      <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"> 
       <Image Source="{Binding FavImage}" Stretch="UniformToFill"/> 
      </Border> 
      <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}"> 
       <TextBlock Text="{Binding FavTitle}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="48" Margin="15,0,15,0"/> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 

我再有这样的HubSection

  <HubSection x:Name="FavHub" Padding="40,60,40,0" > 
      <DataTemplate> 
       <GridView 
        x:Name="itemGridView" 
        Margin="-4,-4,0,0" 
        AutomationProperties.AutomationId="ItemGridView" 
        AutomationProperties.Name="Items In Group" 
        ItemsSource="{Binding Items}" 
        ItemTemplate="{StaticResource Standard240x320ItemTemplateFAV}" 
        SelectionMode="Single" 
        IsSwipeEnabled="false" 
        IsItemClickEnabled="True" 
        ItemClick="ItemView_ItemClick"> 
       </GridView> 
      </DataTemplate> 
     </HubSection> 

我使用此代码添加在DataContext:

FavHub.DataContext = new FavData(Constants.getImage("1002"), "No Favourites"); 

凡FavData类是:

public class FavData 
    { 
     public static string FavImage { get; set; } 
     public static string FavTitle { get; set; } 

     public FavData() { } 

     public FavData(string itemImageSet, string itemNameSet) 
     { 
      FavImage = itemImageSet; 
      FavTitle = itemNameSet; 
     } 
    } 

但是,没有数据显示在HubSection中。我究竟做错了什么?

+1

哪里Items'的'名单:'的ItemsSource = “{绑定表项}”' – WiredPrairie

+0

哦阿福,就是这样。不知道为什么我没有注意到这一点。现在可能是一个可怕的问题,但是,任何想法它应该是什么?谢谢! >。< – ReignOfComputer

+0

'List '将作为一个例子。 'ObservableCollection '另一个。 – WiredPrairie

回答

4

您需要将一个列表(如List<FavData>ObservableCollection<FavData>)绑定到Hub。

现在,您已获得GridView,其中包括许多其他属性,包括ItemsSource属性的初始化。此属性用作项目列表的来源。

<GridView x:Name="itemGridView" 
    ItemsSource="{Binding Items}" 
</GridView> 

被指定为{Binding Items},这意味着对于任何对象当前绑定到集线器,抓住存储在Items属性列表的结合。由于您目前通过DataContext属性将一个FavData实例设置为集线器,并且它没有名为Items的属性,因此无法显示任何内容。

因此,我的建议是创建一个FavData实例的列表,并将其与Hub实例绑定。如果要直接绑定列表而不是将列表存储在另一个“父”对象中,则还需要调整Binding以引用“self”而不是特定的属性。为此,您只需使用语法:{Binding}。它只是意味着,“绑定到我”。因此,GridView将直接在绑定对象(FavData的列表)上查找项目列表。

<GridView x:Name="itemGridView" 
    ItemsSource="{Binding}" 
</GridView> 

和C#:

List<FavData> favs = new List<FavData>(); 
favs.Add(new FavData(Constants.getImage("1002"), "No Favourites")); 
FavHub.DataContext = favs;