Grid Items keep stacking instead of creating a new row

54 Views Asked by At

After I add a new item to a grid it goes to the same square as the last ones. When I click the button more than once instead of making a new row it makes another exercise on top of the last You can see that its bolded. Thats why i think they are overlapping

They are overlappi on the top left square. It should create a new row with the item.

 private void AddExerciseToGroup()
 {
     // Create a new exercise with default values
     var newExercise = new Exercise
     {
         Reps = 0,  // Set default reps
         Weight = 0 // Set default weight
     };

     // Find the selected group based on the selected exercise type
     var selectedGroup = ExerciseTypeGroups.FirstOrDefault(group => group.ExerciseType == SelectedExerciseType);

     // Check if the selected group exists
     if (selectedGroup != null)
     {
         // Add the new exercise to the group's ExercisesInGroup collection
         selectedGroup.ExercisesInGroup.Add(newExercise);
     }
     else
     {
         // If the selected group doesn't exist, create a new group and add the exercise
         var newGroup = new ExerciseTypeGroup
         {
             ExerciseType = SelectedExerciseType,
             ExercisesInGroup = new ObservableCollection<Exercise> { newExercise }
         };

         // Add the new group to the ExerciseTypeGroups collection
         ExerciseTypeGroups.Add(newGroup);
     }

 }
<ViewCell>
 <!-- Display ExerciseType of the group -->
 <StackLayout Margin="0,0,0,0"
              x:Name="ExerciseStack">
     
     <Label Text="{Binding ExerciseType.Name}" FontSize="25" Padding="10" />
        
     
         <Grid x:Name="ExerciseGrid" BindableLayout.ItemsSource="{Binding ExercisesInGroup}">
             
 
             <!-- Entry for Reps -->
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="2*" />
             <ColumnDefinition Width="2*" />
             <ColumnDefinition Width="*" />
         </Grid.ColumnDefinitions>

         <!-- Entry for Reps -->
         <Entry Grid.Row="0" Grid.Column="0" Placeholder="Reps" Keyboard="Numeric" Text="{Binding Exercise.Reps}" />

         <!-- Entry for Volume -->
         <Entry Grid.Row="0" Grid.Column="1" Placeholder="Volume" Keyboard="Numeric" Text="{Binding Exercise.Weight}" />

         <!-- Button to add exercise to the group -->
         <Button Grid.Column="2" Text="Complete" Command="{Binding Source={RelativeSource AncestorType={x:Type ViewModels:WorkoutViewModel}}, Path=CompleteExerciseCommand}" />
     </Grid>
     <Button Grid.Column="1" Text="Add Exercise" Command="{Binding BindingContext.AddExerciseToGroupCommand, Source={x:Reference Workouts}}" VerticalOptions="End"/>
 </StackLayout>
</ViewCell>

1

There are 1 best solutions below

2
Jason On

You have to specify row and col values when adding to a grid, otherwise they default to 0,0. This is difficult to accomplish when using a bindable Grid, and the docs specifically recommend not using BindableLayout with a Grid