0

I've got an issue with binding to a listview.

ViewModel:

public class MainPageViewModel : INotifyPropertyChanged
{

    public MainPageViewModel()
    {

    }

    private ObservableCollection<Player> players;

    public ObservableCollection<Player> Players
    {
        get { return players; }
        set 
        { 
            players = value;                
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Players")); 
        }
    }

    private List<string> playerNames = new List<string>();
    public List<string> PlayerNames
    {
        get { return playerNames; }
        set
        {
            playerNames = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PlayerNames"));
        }
    }

    private GameType selectedGame;

    public GameType SelectedGame
    {
        get { return selectedGame; }
        set 
        {
            selectedGame = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedGame"));
        }
    }

    private string gameName;

    public string GameName
    {
        get { return gameName; }
        set
        {
            gameName = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GameName"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    
}

Player:

public class Player : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name")); }
    }

    private string currentScore = "0";

    public string CurrentScore
    {
        get { return currentScore; }
        set { currentScore = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentScore")); }
    }

    private string gamesWon = "0";

    public string GamesWon
    {
        get { return gamesWon; }
        set { gamesWon = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GamesWon")); }
    }

    //public override string ToString()
    //{
    //    return Name;
    //}


}

ListView:

<ListView x:Name="listviewPlayers" HorizontalOptions="End" ItemsSource="{Binding Players}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Players.Name}" />

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Updating viewmodel:

public void InitGame(Game game)
    {
        //initialize points and setup first player
        viewModel.Players = game.Players;
        viewModel.SelectedGame = game.SelectedGame;
        viewModel.GameName = game.SelectedGame.Name;
        contentMain.BindingContext = viewModel;            
    }

The above ListView bindings don't work - with error : The property 'Name' was not found in type 'ObservableCollection`1'. But if I remove the ItemTemplate and bind the listview's itemssource to {Binding Players[0].Name} the listview displays every name in the List Players. I can't see what I'm doing incorrectly. Would appreciate some help here.

Thanks, Mike

2 Answers 2

0

do this

<Label Text="{Binding Name}" />

the BindingContext for each row is the matching element of the ItemsSource, so in your case it's the individual Player instance

you should also add this

<DataTemplate x:DataType="model:Player" >

where model is an xmlns defined in the header of the XAML

see docs

5
  • I've tried that Jason = I get the following error: Binding: Property "Name" not found on "Game.MainPageViewModel". Name is within the Player Class, the MainPageViewModel has ObservableCollection<Player> Players, so I thought I would be able to do Players.Name or something similar?
    – Mike
    Commented Jan 19, 2024 at 16:40
  • see my edits above
    – Jason
    Commented Jan 19, 2024 at 17:26
  • thanks, I've done that and am now getting some odd errors to do with the Android namespace not being found.
    – Mike
    Commented Jan 19, 2024 at 18:54
  • <ContentPage xmlns="xamarin.com/schemas/2014/forms" xmlns:x="schemas.microsoft.com/winfx/2009/xaml" x:Class="Game.MainPage" Background="Transparent" x:Name="contentMain" xmlns:game="clr-namespace:Game" LayoutChanged="ContentPage_LayoutChanged" Appearing="contentMain_Appearing"> Is this correct? I'm getting the error System.InvalidCastException: 'Specified cast is not valid.', but with no context
    – Mike
    Commented Jan 19, 2024 at 20:21
  • with ListView you can only use ViewCell, TextCell, etc
    – Jason
    Commented Jan 19, 2024 at 20:28
0

Thanks again Jason. That sorted my issues.

1
  • Thanks for your response! If above answer is helpful, please accept it as answer (click the ✔ in the upper left corner of this answer), it will help others who have similar issue. Thanks in advance! Commented Feb 7, 2024 at 1:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.