I am trying to place strings to grid element in xaml, but i am having trouble populating the list from which I take strings. Here is the XAML code:
</Grid.RowDefinitions>
<StackPanel Grid.Column="2" Grid.Row="1">
<TextBlock x:Name="HomeDefender1" TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="2">
<TextBlock x:Name="HomeDefender2" TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="3">
<TextBlock x:Name="HomeDefender3" TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="4">
<TextBlock x:Name="HomeDefender4" TextWrapping="Wrap" Height="100" FontWeight="Bold" FontStyle="Italic" FontSize="10" Foreground="WhiteSmoke"></TextBlock>
</StackPanel>
Each text block is basicaly a position on a grid/canvas where name of a single player is suppose to go. By selecting an element from combo box, here named cb, a method forms an API call by completing URL with combo box element. Here is a method that should place element in these four text blocks, from API. This method works.
public static async void SetHomeTeamPlayersPositionsDEF(ComboBox cb,TextBlock tb1, textBlock tb2, TextBlock tb3, TextBlock tb4)
{
if (cb.SelectedIndex == -1)
{
return;
}
var url = new Url("http://worldcup.sfg.io/matches/country?fifa_code=");
string urlEndpoint = GetItemFromComboBoxWpf(cb);
var request = url + urlEndpoint;
List<Teams.StartingEleven> defenders = new List<Teams.StartingEleven>();
try
{
//await Task.Run(async () =>
//{
if (request != null)
{
List<Teams.RootObject> matches = await request.GetJsonAsync<List<Teams.RootObject>>();
foreach (var match in matches)
{
foreach (var player in match.home_team_statistics.starting_eleven)
{
foreach (var position in player.position)
{
if (player.position.Contains("Defender"))
{
defenders.Add(player);
var firstDef = defenders.ElementAt(0).name;
var secondDef = defenders.ElementAt(1).name;
var thirdDef = defenders.ElementAt(2).name;
var fourthDef = defenders.ElementAt(3).name;
tb1.Inlines.Add(firstDef);
tb2.Inlines.Add(secondDef);
tb3.Inlines.Add(thirdDef);
tb4.Inlines.Add(fourthDef);
}
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Now, here is the problem. Debugging the method, I see that there is only one element contained in the list defenders. There should be four elements in the list, but there is only one player in this list. Method only works if I comment out other variables and use only variable with zero index. So, I can populate only one text block. And here we come to another problem. In the text block, added player's name repeats itself border to border, if textblok has enough space, it is filled with repeated name of a selected player. My goal is to place one name per text block without repeating. Any ideas? Thank you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…