2013-02-19 25 views
2

這是我第一次使用XAML贏得8商店應用程序,所以對於幾件事情不太確定。我想要將數據綁定到一個gridview。要做到這一點,我有一個將組數據綁定到Gridview窗口8商店應用程序

class Category 
{ 
    public int Id { get; set; } 
    public string CategoryName { get; set; } 
    public string IconPath { get; set; } 
} 
代碼

的背後,我有

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) 
    { 
     // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"] 
     Model.Utility util = new Utility(); 
     var categories = util.GetCategoryList(); // this returns List<Category> 
     this.DefaultViewModel["Items"] = categories; 
    } 

,我的XAML是:

<!-- Horizontal scrolling grid used in most view states --> 
    <GridView 
     x:Name="itemGridView" 
     AutomationProperties.AutomationId="ItemsGridView" 
     AutomationProperties.Name="Items" 
     TabIndex="1" 
     Grid.RowSpan="2" 
     Padding="116,136,116,46" 
     ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
     ItemTemplate="{StaticResource Standard250x250ItemTemplate}" 
     SelectionMode="None" 
     IsSwipeEnabled="false"/> 

但我沒有看到任何數據,當我運行應用程序。我在哪裏做錯了?

回答

4

Standard250x250ItemTemplate默認綁定到屬性Title,SubTitle和Image。除非您更新了模板,否則您的Category類沒有這些ItemTemplate的屬性,沒有任何可顯示的內容。我懷疑有數據綁定錯誤VS當您調試應用程序說標題,子標題和圖像屬性無法找到。

要解決此問題,請右鍵單擊GridView,選擇Edit Additonal Templates,Edit Generated Items(ItemTemplate),Edit a Copy並更新模板以將正確的元素綁定到類上的屬性名稱。

+0

這真棒。歡呼m8。 :) 有用。 – kandroid 2013-02-19 20:34:25

1

根據代碼中的一些名稱,看起來您正在嘗試爲Grid App模板重用一些模板代碼。

我也要去假定您在同一XAML文件中定義以下資源:

<CollectionViewSource x:Name="itemsViewSource" Source="{Binding Items}" /> 

如果是這樣,你應該看到矩形您每個類別,但沒有數據。那是因爲您引用了Standard250x250ItemTemplate數據模板(位於StandardStyles.xaml中),並且它正在數據源中尋找具有TitleSubtitle等名稱的特定字段。但對於類別,您有CategoryNameId

試試這個,看看你的數據是否出現。這沒有任何樣式,但你可以複製Standard250x250ItemTemplate的樣式,並根據需要進行調整。你可以通過IDE - Blend或Visual Studio來做到這一點 - 你不必剪切和粘貼XAML。

<GridView 
    x:Name="itemGridView" 
    AutomationProperties.AutomationId="ItemsGridView" 
    AutomationProperties.Name="Items" 
    TabIndex="1" 
    Grid.RowSpan="2" 
    Padding="116,136,116,46" 
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 

    SelectionMode="None" 
    IsSwipeEnabled="false"> 

    <GridView.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding CategoryName}" /> 
     </DataTemplate> 
    </GridView.ItemTemplate> 
</GridView> 
相關問題