© 2026 Hedgehog Software, LLC
System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Controls.Button' to type 'System.Windows.Controls.UIElementCollection'.'
private void addGameToView(Game game) { Button button = new Button(); if (game.Icon == null) { button.Content = game.Name; if (gamesGrid.Children.Count > 0) { foreach (UIElementCollection child in gamesGrid.Children) //ERROR HERE { foreach (UIElement element in child) { Debug.Write(element.ToString()); if (child.GetType() != typeof(Game)) { //Grid.SetColumn(button, Grid.GetColumn(element)); //Grid.SetRow(button, Grid.GetRow(element)); //gamesGrid.Children.Add(button); //break; } } } } else { Grid.SetColumn(button, 0); Grid.SetRow(button, 0); gamesGrid.Children.Add(button); } } }
<Grid Name="gamesGrid" Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> </Grid>