网格视图中的数据绑定

4
我将尝试将一些数据绑定到Windows 8.1的Hub控件中的GridView上。
目前,我在Page.Resources下设置了一个DataTemplate,如下所示:
        <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>

I use this code to add the 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="{Binding Items}" - WiredPrairie
哦,糟糕,就是这样。不知道为什么我之前没注意到。现在可能是一个可怕的问题,但是,你有任何想法应该是什么吗?谢谢!> . < - ReignOfComputer
一个 List<FavData> 可以作为一个例子。ObservableCollection<FavData> 是另一个选择。 - WiredPrairie
抱歉,我对数据绑定还很陌生,不确定如何将上述内容放入XAML中。我应该将ItemSource属性更改为什么? - ReignOfComputer
哦,我建议您将DataContext属性设置为FavData列表,并将绑定更改为{Binding},这意味着绑定到“自身”,而不是一个属性。 - WiredPrairie
1个回答

4
你需要绑定一个列表,比如一个 List<FavData> 或者一个 ObservableCollection<FavData> 到 Hub 上。
现在,你已经有了一个 GridView,其中包括了很多其他属性,其中包括初始化 ItemsSource 属性。这个属性被用作项目列表的数据源。
<GridView x:Name="itemGridView"
    ItemsSource="{Binding Items}"
</GridView>

绑定被指定为{Binding Items},这意味着无论当前绑定到 Hub 的对象是什么,都会获取存储在 Items 属性上的列表。由于您当前通过 DataContext 属性将单个 FavData 实例设置为 Hub,并且它没有名为 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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接