2013-09-10 129 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;