网格项目继续堆叠而不是创建新行

问题描述 投票:0回答:1

将新项目添加到网格后,它会转到与最后一个项目相同的方块。当我多次单击按钮而不是创建新行时,它会在最后一行的基础上进行另一个练习

它们重叠在左上角的正方形上。它应该使用该项目创建一个新行。

 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>

android xaml xamarin
1个回答
0
投票

添加到网格时必须指定行和列值,否则它们默认为 0,0。当使用可绑定的 Grid 时,这是很难实现的,并且 docs 特别建议不要将 BindableLayout 与 Grid 一起使用

© www.soinside.com 2019 - 2024. All rights reserved.