0
votes

I'm having trouble getting my model to show up when trying to databind it in XAML setting Xamarin's BindingContext...here's my simplified model code:

public class LeagueRootObject
{
    public League League { get; set; }
    public ObservableCollection<Match> Matches { get; set; }
    public object Error { get; set; }
}

public class League
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public DateTime PlayingDate { get; set; }
}

public class Match
{
    public MatchDetail MatchDetail { get; set; }
    public ObservableCollection<Player> Players { get; set; }
}

public class MatchDetail
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

public class Player
{
    public PlayerDetail PlayerDetail { get; set; }
    public object Rating { get; set; }
    public int MatchPlayerId { get; set; }
}

public class PlayerDetail
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

and here's simplified XAML snippet:

<ListView ItemsSource="{Binding Path=Matches}" VerticalOptions="StartAndExpand">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.View>
          <StackLayout Orientation="Vertical">
            <Label Text="{Binding Path=MatchDetail.Name}"/>
            <ListView ItemsSource="{Binding Path=Players}">
              <ListView.ItemTemplate>
                <DataTemplate>
                  <TextCell Text="{Binding Path=PlayerDetail.FirstName}"/>
                  <!-- another ViewCell here seems to hide data somehow -->
                </DataTemplate>
              </ListView.ItemTemplate>
            </ListView>
          </StackLayout>
        </ViewCell.View>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

which is placed in the root StackLayout element of the Xamarin ContentPage and whose BindingContext is set to the LeagueRootObject instance loaded properly from json (which I've checked in debugger as working).

The XAML code above is the best I've been able to do since adding Label inside the StackLayout contained in the inner ListView's ViewCell just doesn't show up properly...how can you add a ViewCell (or any other type of cell for that matter) to the inner ListView and build its View so it can show all the data belonging to all of the inner collection objects (I'm currently only getting the first one to show up also which is a problem when there's more than one player)...what am I doing wrong, should a ListView not be used that way and have some other 'View` control be used for outer objects, all I want is to show all inner objects grouped inside outer ones somehow please...

Thanks

2
Seems my json model code and deserialization wasn't working properly, I've used json2csharp to create it and then tweaked it, leaving me with properly loaded matches (assuming it was working right) and nothing in their details...I'm about to recreate it and see if it works...will say if the databinding code works then.GI1
I managed to properly load the inner object now (since I tweaked the names I forgot to add JsonProperty attributes) and it could show some elements now using GroupHeaderTemplate but the inner ItemTemplate don't still show up, so the question stands...what is wrong with the XAML code above?GI1
With a ListView you don't use the BindingContext but ItemsSourceGusman
Thanks for your comment but I don't see how using BindingConext of the ListView-containing ContentPage instead of directly setting it's ItemSource is a really bad thing..given outer Collection shows I think it's fine..is it not?GI1
To be honest never saw nobody trying to set the bindingcontext of the listview, and every documentation on Xamarin site and examples only use the ItemsSourceGusman

2 Answers

0
votes

In addition to me maybe being bad at Xamarin-flavoured XAML you also maybe right about there being bugs in the way the listviews work...since I just tried the above code on Windows 10 mobile and there it would not show any data of the inner collection but running it on Android worked by showing multiple inner collection last names...and after scrolling down past that outer collection object it would crash the app.

So, this may not be the way to show such a list of inner objects (and something like a button action beside each of them) grouped by belonging to outer collection onjects, but I still can't find another one example in XAML.

Could it be that you can only do that in foreach looped code in Xamarin.Forms 2.0?

0
votes

If I understood correctly your issue, I could suggest you solution that helped me to show ListView in ListView.

https://stackoverflow.com/a/35598179/5912513