列表中的数据绑定列表,uwp + xaml

时间:2018-04-30 15:48:48

标签: c# json xaml uwp uwp-xaml

我有以下型号:

public class TableData
{
    [JsonProperty(PropertyName = "objectsDetected")]
    public List<ObjectsDetected> ObjectsDetected { get; set; }

    [JsonProperty(PropertyName = "file_name_at_upload")]
    public int File_Name_At_Upload { get; set; }
}

public class ObjectsDetected
{
    [JsonProperty(PropertyName = "className")]
    public string className { get; set; }

    [JsonProperty(PropertyName = "score")]
    public double score { get; set; }
}

我从api电话中获取数据:

        var request = (HttpWebRequest)WebRequest.Create("http://localhost:58941/api/data");
        request.Method = "GET";
        request.ContentType = "application/json";
        WebResponse response = await request.GetResponseAsync();

        if (response != null)
        {
            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            List<TableData> myDeserializedObjList = (List<TableData>)JsonConvert.DeserializeObject(responseString, typeof(List<TableData>));
            cosmosData.ItemsSource = myDeserializedObjList;

        }

这是XAML代码:

    <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="Navn på billede" Foreground="Black" FontSize="20" FontWeight="Bold"/>
            <TextBlock Grid.Column="2" Text="Kategori fundet" Foreground="Black" FontSize="20" FontWeight="Bold"/>
        </Grid>
            <GridView x:Name="cosmosData"
                      ItemClick="cosmosData_ItemClick"
                      IsItemClickEnabled="True"
                      IsSwipeEnabled="true"
                      SelectionMode="Single">
                <GridView.ItemTemplate>
                <DataTemplate x:DataType="local:TableData">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{x:Bind File_Name_At_Upload}" />
                        <TextBlock Grid.Column="2" Text="{x:Bind ObjectsDetected.className}"/>

                    </Grid>
                </DataTemplate>

            </GridView.ItemTemplate>
            </GridView>
    </StackPanel>

我想在同一个表中的用户界面TableData和ObjectsDetected.classname中显示,但我无法访问ObjectsDetected.classname?我怎样才能实现这一目标?

1 个答案:

答案 0 :(得分:0)

由于您希望为每个TableData显示多个类名,因此您需要创建所有类名的复合字符串,或者使用ItemsControl来显示它们。后者不是那么高效,但在XAML中也很容易实现,如下所示:

<GridView x:Name="cosmosData" ItemClick="cosmosData_ItemClick" IsItemClickEnabled="True" IsSwipeEnabled="true" SelectionMode="Single">
<GridView.ItemTemplate>
    <DataTemplate x:DataType="local:TableData">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{x:Bind File_Name_At_Upload}" />
            <ItemsControl Grid.Column="1" ItemsSource="{x:Bind ObjectsDetected}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding className}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </DataTemplate>
</GridView.ItemTemplate>

作为替代选项,您可以在TableData模型上添加另一个属性,如下所示:

public class TableData
{
    [JsonProperty(PropertyName = "objectsDetected")]
    public List<ObjectsDetected> ObjectsDetected { get; set; }

    [JsonProperty(PropertyName = "file_name_at_upload")]
    public int File_Name_At_Upload { get; set; }

    [JsonIgnore]
    public string ClassNames => String.Join(",", ObjectsDetected.Select(o => o.className));
}

然后在原始代码中,绑定到ClassNames属性。 (这将以逗号加入值,但您可以使用\n代替,将每个值放在自己的行上。您还需要在TextBlock中设置TextWrapping