I have a class that contains different characteristics for a pokemon
public ObservableCollection<Pokemon> GetMesPokemons()
{
ObservableCollection<Pokemon> lstPokemons =
new ObservableCollection<Pokemon>();
lstPokemons.Add(new Pokemon()
{
NamePokemon = "Raichu",
UrlImage = "https://i0.wp.com/pokemon-suisse.ch/wp-content/uploads/2021/06/img_1517.jpg?resize=1536,2048&ssl=1",
Abilities = new List<Ability>()
{
new Ability
{
Name = "Thunderbolt",
NbPower = 120
},
new Ability
{
Name = "Spark Ball GX",
NbPower = 200
}
},
PokemonClass = new PokemonType()
{
NameClass = "Electric",
UrlImage = "https://th.bing.com/th/id/OIP.T-kNLsS_VhinWEuPIfZdHAHaHk?w=159&h=180&c=7&r=0&o=5&pid=1.7"
},
Pv = 210
I can get the name of the pokemon and its image and pv, but when I try to get its "Abilities" I get this message on the display: System.Collections.Generic.List'1[MauiApp2.Models.Ability]
This is how I try to get it back
<CollectionView
x:Name="PokemonsAbilities"
ItemsSource="{Binding AbilitiesSource}"
ItemTemplate="{StaticResource AbilitiesTemplate}">
</CollectionView>
<!--Récupération de l'image d'un pokemonRandom-->
<Image Source="{Binding SelectedPokemon.UrlImage}" WidthRequest="150" HeightRequest="150"/>
<!--Récupération du nom d'un pokemonrandom-->
<Label
Text="{Binding SelectedPokemon.NamePokemon}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label
Text="{Binding SelectedPokemon.Pv}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label
Text="{Binding SelectedPokemon.Abilities}"
VerticalOptions="Center"
HorizontalOptions="Center" />
This is my Pokemon model
public class Pokemon
{
private string _namePokemon;
public string NamePokemon
{
get { return _namePokemon; }
set { _namePokemon = value; }
}
private string _urlImage;
public string UrlImage
{
get { return _urlImage; }
set { _urlImage = value; }
}
private int _pv;
public int Pv
{
get { return _pv; }
set { _pv = value; }
}
public List<Ability> Abilities { get; set; }
This is my Ability model
public class Ability
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _nbPower;
public int NbPower
{
get { return _nbPower; }
set { _nbPower = value; }
}
My template
<DataTemplate x:Key="LstPokemons">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!--<Button Text="Profil"></Button>-->
<Image Source="{Binding UrlImage}" Grid.Column="2" WidthRequest="150" HeightRequest="150"></Image>
<Label Text="{Binding NamePokemon}" Grid.Column="0"></Label>
</Grid>
</DataTemplate>
<DataTemplate x:Key="Abilities">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
<ColumnDefinition Width="0.5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}" />
<Label Text="{Binding NbPower}" />
</Grid>
</DataTemplate>
My "PokemonView" page representing my two View collections based on my two DataTemplates one to retrieve the list of pokemons and the other to retrieve the abilities of a pokemon
<ContentPage.BindingContext>
<vm:PokemonViewModel></vm:PokemonViewModel>
</ContentPage.BindingContext>
<VerticalStackLayout>
<Label
Text="{Binding Pseudo}"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Label Text="Voici les pokemons disponible pour un match"></Label>
<!--Collectionview : qui contient mon template pour afficher une grid qui va contenir des pokémons
On peut en sélectionner un à la fois-->
<CollectionView
x:Name="Pokemons"
ItemTemplate="{StaticResource LstPokemons}"
ItemsSource="{Binding ListPokemons}"
SelectionMode="Single"
SelectedItem="{Binding MonSelectedPokemon}">
</CollectionView>
<CollectionView
x:Name="PokemonsAbilities"
ItemTemplate="{StaticResource AbilitiesTemplate}"
ItemsSource="{Binding AbilitiesSource}">
</CollectionView>
<!--Boutton pour passer a la page suivante une fois qu'il a séléctionné un pokémon-->
<Button x:Name="btnValidate" Text="Valider" Command="{Binding ValidateSelected}"></Button>
</VerticalStackLayout>
</ContentPage>
CodePudding user response:
You need a property in your model that will convert your list of Ability objects into a string that you can display in a Label
public string AbilitiesDesc
{
get
{
return string.Join(",", Abilities.Select(a => a.Name));
}
}
Then bind this new property to the Label
<Label
Text="{Binding SelectedPokemon.AbilitiesDesc}"
VerticalOptions="Center"
HorizontalOptions="Center" />