English 中文(简体)
在点击tom x子Xaml之后如何在名单上添加一行
原标题:How to make a row in a list after clicking a buttom xaml

I want to make a fitness app which keeps your workouts. Im trying to make a list in which you can add your exercises and sets reps and weight but it didnt work. I tried to make a counter (sets) which increments every time the button is pressed but didnt manage to. View


  
        <!-- 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>

ViewModel

 public ICommand AddExerciseToGroupCommand => new Command(AddExerciseToGroup);

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

“Thatats<<>>>

问题回答

页: 1 因此,你应当使用Relative Bleds

Option 1 Use Relative Bindings

If you use Relative Bindings, you may refer to Relative bindings. Make some changes to the Command Binding,

<!-- 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"/>

Option 2 Use x:reference

You could also use x:reference Binding expression to achieve,

第一,增加<> 内容提要的财产,

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

接着,Bled指挥部试图这样做。

<!-- 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"/>

希望!





相关问题
How to bind a TabControl to an ObservableCollection in XAML

I have the following line of code in my code-behind class. TabControl.ItemsSource = ((MainWindowViewModel)DataContext).TabItemViewModels; I would like to move this to the XAML file. In brief, ...

Bind to ancestor of adorned element

Here is the case: <DataTemplate x:Key="ItemTemplate" DataType="local:RoutedCustomCommand"> <Button Command="{Binding}" Content="{Binding Text}" ...

FlowDocument Force a PageBreak (BreakPageBefore)

I m using C# to create a FlowDocument and fill it with data within a table. Example: FlowDocument flowDoc = new FlowDocument(); Table table1 = new Table(); flowDoc.Blocks.Add(table1); table1....

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF TreeView binding through XAML

So I have a class structure like this Store Owner Cashier List of Products Product Name Price List of Vendors Company Name Contact Name So there are 4 ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签