Xamarin表单ListView绑定到Observable集合中的子对象列表

时间:2018-02-14 16:39:59

标签: listview xamarin.forms

如果答案很明显,请原谅这一点。我对Xamarin和Forms相当新,但不是编程或C#。 我有一个ListView,它可以很好地绑定到顶级对象及其直接属性。它是GameDTOModel对象的ObservableCollection。

public ObservableCollection<GameDTOModel> ActiveGamesCollection { get; } = new ObservableCollection<GameDTOModel>();

GameDTOModel是这样的:

public class GameDTOModel 
{
    public int Id { get; set; }

    public string Name { get; set; }

    // Which Game type is used in this game
    public GameType GameType { get; set; }    
}

GameType看起来像这样:

public class GameType
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Prize> Prizes { get; set; }
}

和Prize对象是这样的:

public class Prize
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string ImageURL { get; set; }
}

到目前为止,这么简单,就像我说的那样。我没有问题到ActiveGamesCollection中的对象并将其绑定到ListView。 我希望能够在Prize对象的ImageURL属性中显示图像。因此,例如,游戏可能具有3个奖品的GameType,因此它是列表。 我的XAML看起来像这样:

<ListView x:Name="GamesView" ItemSelected="GamesViewItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout BackgroundColor="#eee" Orientation="Vertical">
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding ...stuff...}" />
                        <Label Text="{Binding ...stuff...}" />
                        <Label Text="{Binding ...stuff...}" />
                        <Label Text="{Binding ...stuff...}" />
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
<StackLayout Orientation="Horizontal">
    <Label Text="Loaded "/>
    <Label Text="{Binding ActiveGamesCollection.Count}"/>
    <Label Text="  games from the server @ "/>
    <Label Text="{Binding DebugmsgServerURL}"/>
</StackLayout>

在后面的代码中设置了Itemsource和其他东西:

GamesView.ItemsSource = gamesmodel.ActiveGamesCollection;
GamesView.SelectedItem = gamesmodel.SelectedGame;
GamesView.IsPullToRefreshEnabled = true;

我在List对象中也有一个ActiveGames的副本,但需要可观察的集合才能填充ListView。 (看来)。

我希望能够在ListView中列出3个图像,但我很难绑定到ActiveGamesCollection.GameType.Prizes,因为它是可观察的集合。 另外我从阅读中看到你不应该在listview中嵌套listview,所以我也需要一个解决方案。我想我可以使用gridview,但只是到达它们将是一个开始。

有人指点吗? 感谢。

1 个答案:

答案 0 :(得分:1)

您可以在ViewCell中使用 DynamicWrapLayout 来显示图片。

<suave:DynamicWrapLayout ItemsSource="{Binding Items}" HorizontalOptions="Fill">
        <suave:DynamicWrapLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout BackgroundColor="Gray" WidthRequest="120" HeightRequest="180">
                    <Label Text="{Binding .}" TextColor="White" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" />
                </StackLayout>
            </DataTemplate>
        </suave:DynamicWrapLayout.ItemTemplate>
</suave:DynamicWrapLayout>
  

参考https://github.com/SuavePirate/DynamicWrapLayout