【问题标题】:Data Binding in GridViewGridView 中的数据绑定
【发布时间】:2013-09-11 23:08:57
【问题描述】:

我正在尝试将一些数据绑定到 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>

我使用此代码添加 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 中没有显示任何数据。我做错了什么?

【问题讨论】:

  • Items的名单在哪里:ItemsSource="{Binding Items}"
  • 哦,就是这样。不知道为什么我之前没有注意到这一点。现在可能是一个可怕的问题,但是,知道它应该是什么吗?谢谢! >.
  • A List&lt;FavData&gt; 可以作为示例。 ObservableCollection&lt;FavData&gt; 另一个。
  • 对不起,我对数据绑定真的很陌生,不知道如何将上述内容放入 XAML。我究竟要将 ItemSource 属性更改为什么?
  • 哦,我建议您将DataContext 属性设置为FavData 的列表,并将绑定更改为{Binding},这意味着绑定到“自我”,而不是属性。

标签: c# data-binding windows-runtime winrt-xaml windows-8.1


【解决方案1】:

您需要将一个列表(例如 List&lt;FavData&gt;ObservableCollection&lt;FavData&gt;)绑定到 Hub。

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

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

绑定被指定为{Binding Items},这意味着对于当前绑定到集线器的任何对象,获取存储在Items 属性上的列表。由于您当前已通过 DataContext 属性为 Hub 设置了一个 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2015-04-29
    相关资源
    最近更新 更多