I have been trying to populate my picker but I don't know how. I have looked at some tutorial online but they are kind of confusing and they all create a list of the items which I haven't done. `New Game Class
namespace FYP.ViewModels
{
public class NewGames
{
public int Id { get; set; }
public string GameTitle { get; set; }
public double Rating { get; set; }
public string ImageSource { set; get; }
}
}
`using System.ComponentModel; using System.Text;
namespace FYP.ViewModels {
public class NewReleasesViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<NewGames> NewGames;
public ObservableCollection<NewGames> Games
{
get { return NewGames; }
set { NewGames = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Games"));
}
}
public NewReleasesViewModel()
{
Games = new ObservableCollection<NewGames>();
AddData();
}
private void AddData()
{
Games.Add(new NewGames
{
Id = 0,
GameTitle = "The Elder Scrolls Online",
Rating = 4.9,
ImageSource= "https://assets-prd.ignimgs.com/2022/01/05/elderscrollsonline- 1641413357689.jpg"
});
Games.Add(new NewGames
{
Id = 1,
GameTitle = "World Of Warcraft",
Rating = 4.9,
ImageSource = "https://assets-prd.ignimgs.com/2021/12/10/wow-1639126324635.jpg"
});
Games.Add(new NewGames
{
Id = 2,
GameTitle = "Star Wars: The Old Republic",
Rating = 4.9,
ImageSource = "https://assets-prd.ignimgs.com/2022/01/27/swotor-sq1-1643302998212.jpg"
});
}
}
} `
<ContentPage.Content>
<StackLayout>
<Label Text="Select Game" TextColor="Black" FontSize="24" FontAttributes="Bold" Margin="15" />
<Picker>
</Picker>
</StackLayout>
</ContentPage.Content>
I'm trying to do something like this where the user picks a title and then something else loads on the screen (https://i.stack.imgur.com/0nMuR.png)
CodePudding user response:
you need to assign an ItemSource
<Picker ItemsSource="{Binding Games}" ... />
and you need to tell it which property to display
ItemDisplayBinding="{Binding GameTitle}" ...
CodePudding user response:
Turns out I was missing the binding context
<ContentPage.BindingContext>
<viewmodels:NewReleasesViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="Select Game" TextColor="Black" FontSize="24" FontAttributes="Bold" Margin="15" />
<Picker ItemsSource="{Binding Games}" ItemDisplayBinding="{Binding GameTitle}"/>
</StackLayout>
</ContentPage>