【问题标题】:How to add Items to GridView in C# Windows Store App (Win8)如何在 C# Windows Store App (Win8) 中将项目添加到 GridView
【发布时间】:2012-10-31 12:33:15
【问题描述】:

为了简单起见,假设我有一个适用于 Windows 8 的 c# Windows Store 项目,其中包含以下项目:

  • GridView (PlatformsGrid)
  • 列表«PlatformSearchResult»(所有平台)
  • standardstyles.xaml 中的数据模板 (PlatformDataTemplate)

allPlatforms 是从在线 API 填充的“PlatformSearchResult”对象的集合,具有以下 3 个属性:

  1. 身份证
  2. 姓名
  3. 别名

我可以为我的 allPlatforms 集合中存在的每个对象向 gridview 添加一个新项目,但是这些项目是空白的,并且不显示来自我的对象的数据。

当前代码的快速摘要如下所示:

XAML 标记:

<!-- Platforms Content -->
<GridView x:Name="PlatformsGrid" Grid.Row="1"
  CanReorderItems="True" CanDragItems="True" 
  ItemTemplate="{StaticResource PlatformDataTemplate}" >
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapGrid MaximumRowsOrColumns="2" 
              VerticalChildrenAlignment="Top" HorizontalChildrenAlignment="Center" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
</GridView>

数据模板

<!-- Platform Item Template -->
<DataTemplate x:Key="PlatformDataTemplate">
    <Grid Background="#FF939598" Height="250" Width="250">
        <Image Source="/SampleImage.png"  Stretch="UniformToFill"/>
        <StackPanel Orientation="Vertical" Background="#CC000000" 
                Height="90" VerticalAlignment="Bottom">
            <TextBlock Text="{Binding Name}" 
                   Margin="10,3,0,0" Width="242" Height="62" 
                   TextTrimming="WordEllipsis" TextWrapping="Wrap" HorizontalAlignment="Left"/>
            <TextBlock Text="{Binding Alias}" 
                   Margin="10,2,0,0" Width="186" Height="14" 
                   TextTrimming="WordEllipsis" HorizontalAlignment="Left" FontSize="9" Opacity="0.49"/>
        </StackPanel>
    </Grid>
</DataTemplate>

控制功能

    private async void FetchItemInfo_Loaded(object sender, RoutedEventArgs e)
    {
        // Get List of Top Games
        List<PlatformSearchResult> allPlatforms = new List<PlatformSearchResult>();
        allPlatforms = await GamesDB.GetPlatforms();

        // Dynamically Create Platform Tiles
        foreach (PlatformSearchResult platform in allPlatforms)
        {
            PlatformsGrid.DataContext = platform;
            PlatformsGrid.Items.Add(platform);
        }
    }

如何获取添加的项目以显示适当的对象属性(暂时忽略图像),我只是对填充 TextBlocks 的内容感兴趣。

感谢任何人提供的任何帮助!

谢谢,亚历克斯。

【问题讨论】:

    标签: c# xaml binding datatemplate windows-store-apps


    【解决方案1】:

    你需要做的就是控制功能

    private async void FetchItemInfo_Loaded(object sender, RoutedEventArgs e)
    {
        // Get List of Top Games
        List<PlatformSearchResult> allPlatforms = new List<PlatformSearchResult>();
        allPlatforms = await GamesDB.GetPlatforms();
    
        PlatformsGrid.ItemsSource = allPlatforms;    
    }
    

    【讨论】:

      【解决方案2】:

      通过属性将allplatforms列表绑定到gridview作为itemssource。当然,这假定 gridview 或页面的数据上下文是包含您的 allplatforms 属性的类。如果没有,您也必须这样做。

      通过后面的代码设置数据上下文

      this.grid.DataContext = class()//

      或通过绑定

      <!-- Platforms Content -->
      <GridView x:Name="PlatformsGrid" Grid.Row="1"
        CanReorderItems="True" CanDragItems="True" 
        ItemsSource = "{Binding AllPlatforms}"
        ItemTemplate="{StaticResource PlatformDataTemplate}" >
          <GridView.ItemsPanel>
              <ItemsPanelTemplate>
                  <WrapGrid MaximumRowsOrColumns="2" 
                    VerticalChildrenAlignment="Top" HorizontalChildrenAlignment="Center" />
              </ItemsPanelTemplate>
          </GridView.ItemsPanel>
      </GridView>
      

      您可以查看 msdn 上提供的 Windows 8 应用示例。

      编辑:您的平台对象必须具有您已添加为绑定的名称和别名属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-19
        相关资源
        最近更新 更多