I am populating a grid using a list of data from view model. But I'm not able to select an item from the grid. Can you guys please help.
<StackPanel Margin="0,69,0,0">
<GridView ItemsSource="{Binding GamesList}" x:Name="gameGrid" >
<GridView.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding Source}" Command="{Binding Path=DataContext.GameSelectionMethod, ElementName=gameGrid}"
CommandParameter="{Binding SelectedValue}" >
<StackPanel Width="202" VerticalAlignment="Top">
<Image Source="{Binding imageurl}" Height="324"/>
<StackPanel Background="Black" Padding="19,9,0,0">
<TextBlock FontWeight="Semibold" TextTrimming="CharacterEllipsis" FontFamily="Segoe Pro"
Foreground="White" TextAlignment="Left" FontSize="24" Text="{Binding title}"
TextWrapping="Wrap" Height="65"/>
<TextBlock FontFamily="Segoe Pro" Foreground="White" TextAlignment="Left"
FontSize="16" Text="{Binding price}" Margin="0,48,0,0"/>
</StackPanel>
</StackPanel>
</Button>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
I need a click event in this button and need to know which one is selected. I have tried many methods but everything is actually complex solution. I have added a command and its not triggering. its working on mouse click but not on gamepad of xbox
My view model
public List<GameModel> GamesList
{
get
{
return gamesList;
}
set
{
gamesList = value;
OnPropertyChanged("GamesList");
}
}
public void GetSampleData()
{
var data = new GameModel { gameId = 1, title = "Call Of Duty: Black Ops 4", price = "$59.99", imageurl = "/Assets/Product Image-7.png" };
GamesList.Add(data);
var data1 = new GameModel { gameId = 2, title = "Forza Horizon 4", price = "$59.99", imageurl = "/Assets/Product Image-8.png" };
GamesList.Add(data1);
var data2 = new GameModel { gameId = 3, title = "Halo 5: Guardians", price = "$59.99", imageurl = "/Assets/Product Image-9.png" };
GamesList.Add(data2);
var data3 = new GameModel { gameId = 4, title = "Sea of Thieves", price = "$59.99", imageurl = "/Assets/Product Image-10.png" };
GamesList.Add(data3);
var data4 = new GameModel { gameId = 5, title = "Assassin's Creed : Origins", price = "$59.99", imageurl = "/Assets/Product Image-11.png" };
GamesList.Add(data4);
var data5 = new GameModel { gameId = 6, title = "Shadow of War", price = "$59.99", imageurl = "/Assets/Product Image-12.png" };
GamesList.Add(data5);
GamesList.Add(data10);
}
public ICommand GameSelectionMethod
{
get
{
return new DelegateCommand(GameSelection);
}
}
private void GameSelection()
{
string tag = "1";
var parameters = new GameModel();
parameters.gameId = Convert.ToInt16(tag);
_navigationService.Navigate(typeof(HomeView), parameters);
}