在网格视图中显示具有UWP中特定功能的数据

时间:2018-01-03 02:59:41

标签: c# json gridview uwp

我有一个GridView,其数据来自JSON,如下所示: gridview

我想如果file1.Size = 0 MB,那么数据不会显示在GridView上。

如何申请?

XAML

<GridView
    x:Name="itemGridView"
    AutomationProperties.AutomationId="ItemDetailScrollViewer"
    DataContext="{Binding SelectedItem, ElementName=itemListView}">

    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid Height="270" Width="155" Margin="10,10,0,0" Background="White" BorderBrush="#FFC3C3C3" BorderThickness="0.5">

                <Border x:Name="coverBox" Grid.Row="0" Margin="10,10,0,0" Width="125" Height="175" VerticalAlignment="Top" HorizontalAlignment="Left" BorderBrush="#FF2E2D9B" BorderThickness="1">
                    <Border.Background>
                        <ImageBrush Stretch="Uniform" ImageSource="images/Kipin-placeholder.png"/>
                    </Border.Background>
                    <Image x:Name="cover" Source="{Binding Cover}" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" AutomationProperties.Name="{Binding Judul}"/>
                </Border>
                <ScrollViewer x:Name="ketMajalah" Grid.Row="1" Background="{x:Null}" Margin="10,5,10,10" Width="125" HorizontalAlignment="Left" HorizontalScrollMode="Disabled" VerticalScrollMode="Auto">
                    <TextBlock x:Name="title" Text="{Binding Judul}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="Black" FontSize="14" Margin="0,0,10,10" FontWeight="SemiBold"/>
                </ScrollViewer>

                <TextBlock Grid.Row="2" Margin="10,0,10,10" Text="{Binding Size}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="Black" FontSize="14"/>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

代码:

JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
JsonArray jsonData1 = jsonObject1["data"].GetArray();

foreach (JsonValue groupValue1 in jsonData1)
{
    JsonObject groupObject2 = groupValue1.GetObject();

    string title = groupObject2["judul"].GetString();
    string cover = groupObject2["cover"].GetString();
    string size = groupObject2["formated_size"].GetString();

    Buku file1 = new Buku();
    file1.Judul = title;
    file1.Cover = cover;
    file1.Size = size;

    datasource.Add(file1);
}

itemGridView.ItemsSource = datasource;

1 个答案:

答案 0 :(得分:0)

我认为你可以删除datasource中的文件大小为0。

                       foreach (JsonValue groupValue1 in jsonData1)
                        {
                            JsonObject groupObject2 = groupValue1.GetObject();

                            string title = groupObject2["judul"].GetString();
                            string cover = groupObject2["cover"].GetString();
                            string size = groupObject2["formated_size"].GetString();

                            Buku file1 = new Buku();
                            file1.Judul = title;
                            file1.Cover = cover;
                            file1.Size = size;

                            datasource.Add(file1);
                        }

        for (var i = 0; i < datasource.Count; i++)
        {
            if (datasource[i].Size == 0)
            {
                datasource.RemoveAt(i);
                i--;
            }
        }