[ad_1]
I just followed this tutorial https://www.youtube.com/watch?v=_4qej7q6x30 on how to create a Responsive Horizontal ListView, the problem is that I don’t know how to get the selected item to run specific code.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="1" Grid.Row="1">
<StackPanel>
<TextBlock Text="Games" FontSize="40" Foreground="White" TextAlignment="Center"/>
<StackPanel Margin="10 0">
<ItemsControl x:Name="games_list" ItemsSource="{Binding games}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="10" Width="128" Height="128">
<StackPanel>
<Border Width="100" Height="100" CornerRadius="25" Margin="5">
<Border.Background>
<ImageBrush ImageSource="{Binding Image_path}"/>
</Border.Background>
</Border>
<TextBlock Text="{Binding Name}" Foreground="White" TextAlignment="Center" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Grid>
This is my grid. It works as it should but I don’t know how to check which item I clicked, I need it to run
var item = ???;
if (item != null)
{
try
{
GameTemplate game = ((GameTemplate)item);
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = game.Run_command;
if (game.Run_arguments != null)
processStartInfo.Arguments = $"\"{game.Run_arguments}\"";
Process.Start(processStartInfo);
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(ex.Message);
sb.AppendLine();
sb.AppendLine(ex.StackTrace);
sb.AppendLine();
sb.AppendLine(ex.Source);
MessageBox.Show(sb.ToString());
}
}
This casts the item to a custom class with specific values
[ad_2]