I am working on my project and I have encountered a problem. I don't know how to show data from a List
.
Code-behind:
public ObservableCollection<GameResult> GameResultsToShow { get; set; } = new ObservableCollection<GameResult>();
public void SortResults()
{
List<GameResult> SortedGameResults; //to bind
if (gameOption.gameType == GameType.Time)
SortedGameResults = GameResults.FindAll(x => x.gameOption.gameLevel == gameOption.gameLevel && x.gameOption.gameType == gameOption.gameType).OrderBy(x => x.points).ToList();
else
SortedGameResults = GameResults.FindAll(x => x.gameOption.gameLevel == gameOption.gameLevel && x.gameOption.gameType == gameOption.gameType).OrderBy(x => x.Time).ToList();
ObservableCollection<GameResult> GameResultsToShow =new ObservableCollection<GameResult>(SortedGameResults);
}
Xaml:
<CollectionView
ItemsSource="{Binding GameResultsToShow }"
BackgroundColor="PapayaWhip"
Margin="10"
Grid.Row="5"
Grid.ColumnSpan="3"
HorizontalOptions="Center">
<CollectionView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding GameResult}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
CodePudding user response:
ListView
can use Cell controls such as TextCell
, ViewCell
, etc. However, CollectionView
can't use Cell controls. Here's the sample code below for your reference:
Xaml:
<ListView
ItemsSource="{Binding GameResultsToShow }"
BackgroundColor="PapayaWhip">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding GameResultValue}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code-behind:
public ObservableCollection<GameResult> GameResultsToShow { get; set; }
public MainPage()
{
InitializeComponent();
//Binding the itemsource as Jason suggested
GameResultsToShow = new ObservableCollection<GameResult>
{
new GameResult{GameResultValue = "Win"},
new GameResult{GameResultValue = "Lose"},
};
BindingContext = this;
}
Model:
public class GameResult
{
public string GameResultValue { get; set; }
}