单击按钮 xaml 后如何在列表中生成一行

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

我想制作一个健身应用程序来记录你的锻炼情况。我试图列出一个列表,您可以在其中添加练习并设置次数和重量,但它不起作用。我尝试制作一个计数器(设置),每次按下按钮时该计数器都会递增,但没有成功。 查看


  
        <!-- Display ExerciseTypeGroups -->
        <ListView ItemsSource="{Binding ExerciseTypeGroups}" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!-- Display ExerciseType of the group -->
                        <StackLayout Margin="0,0,0,0">
                            <Label Text="{Binding ExerciseType.Name}" FontSize="25" Padding="10" />
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

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

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

                                <!-- Button to add exercise to the group -->
                                <Button Grid.Column="2" Text="Complete" Command="{Binding CompleteExerciseCommand}" />
                            </Grid>
                            <Button Grid.Column="1" Text="Add Exercise" Command="{Binding AddExerciseToGroupCommand}" VerticalOptions="End"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

视图模型

 public ICommand AddExerciseToGroupCommand => new Command(AddExerciseToGroup);

 private void AddExerciseToGroup()
 {
    
     OnPropertyChanged(nameof(ExerciseTypeGroups));
 }

xaml xamarin.android
1个回答
0
投票

您的按钮在 ListView 中的

DataTemplate
中定义。所以你应该使用 相对绑定x:引用表达式

使用相对绑定

如果您使用相对绑定,您可以参考相对绑定。对命令绑定进行一些更改,

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

使用x:参考

你也可以使用 x:reference 绑定表达式来实现,

首先,为 ContentPage 添加 Name 属性,

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             ...
             x:Name="MyPage"> 

然后对于命令绑定,试试这个,

<!-- Button to add exercise to the group -->
<Button Grid.Column="2" Text="Complete" Command="{Binding CompleteExerciseCommand, Source={x:Reference MyPage}}" />
    ...                    
<Button Grid.Column="1" Text="Add Exercise" Command="{Binding AddExerciseToGroupCommand, Source={x:Reference MyPage}}" VerticalOptions="End"/>

希望有帮助!

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